文章预览
前言
Django的增上改查的操作都是基于ORM的
什么是ORM?
即Object-Relationl Mapping
,它的作用是在关系型数据库和对象之间作一个映射,这样,我们在具体的操作数据库的时候,就不需要再去和复杂的SQL语句打交道,只要像平时操作对象一样操作它就可以了 。
一、需求分析
实现下表的增删改查的操作
二、项目目录结构
myapp
是项目djangoProject
的APP应用
在djangoProject
中的settings.py
注册应用
在djangoProject
中的urls.py
配置路由
以下的操作都是在myapp目录中的文件进行操作的
三、设计模型类(数据库表)models.py
from django.db import models
# Create your models here.
class Image(models.Model):
id = models.IntegerField('编号',primary_key=True,default='')
name = models.CharField('名称',max_length=50,default='')
url = models.CharField('地址',max_length=256,default='')
is_active = models.BooleanField('是否删除',default=True)
四、路由urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('all_image',views.all_image),
path('update_image/<int:image_id>',views.update_image),
path('delete_image',views.delete_image)
]
五、视图views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from .models import Image
# Create your views here.
#查询所有
def all_image(request):
#查询所有图片信息
# all_image = Image.objects.all()
all_image = Image.objects.filter(is_active=True)
return render(request,'myapp/all_image.html',locals()) #locals()返回搜索变量数据
#更新信息
def update_image(request,image_id):
#查信息
try:
image = Image.objects.get(id=image_id,is_active=True)
except Exception as e:
print('--update image error is %s'%(e))
return HttpResponse('--The image is not existed')
if request.method == 'GET':
return render(request,'myapp/update_image.html',locals())
elif request.method == 'POST':
#取数据
name = request.POST['name']
url = request.POST['url']
#改数据
image.url = url
image.name = name
#存数据
image.save()
return HttpResponseRedirect('/myapp/all_image')
# 删除信息
def delete_image(request):
image_id = request.GET['image_id']
if not image_id:
return HttpResponse("---请求异常")
try:
image = Image.objects.get(id=image_id,is_active=True)
except Exception as e:
print('---delete Image get error is %s'%{e})
return HttpResponse('delete image is error!')
#删除操作
image.is_active = False
image.save()
#跳转
return HttpResponseRedirect('/myapp/all_image')
六、html页面
6.1、all_image.html
展示所有信息
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Images</title>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>url</th>
</tr>
{% for image in all_image %}
<tr>
<td>{{ image.id }}</td>
<td>{{ image.name }}</td>
<td>{{ image.url }}</td>
<td>
<a href="/myapp/update_image/{{ image.id }}">更新</a>
<a href="/myapp/delete_image?image_id={{ image.id }}">删除</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
6.2、update_image.html
更新单条记录
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>更新信息姓</title>
</head>
<body>
<form action="/myapp/update_image/{{ image.id }}" method="post">
<p>
ID:<input name="id" type="text" value="{{ image.id }}" disabled>
</p>
<p>
NAME:<input type="text" name="name" value="{{ image.name }}">
</p>
<p>
URL:<input type="text" name="url" value="{{ image.url }}">
</p>
<p><input type="submit" value="更新"></p>
</form>
</body>
</html>