4、django操作单表进行增删改
1、django初始化配置
https://blog.51cto.com/yht1990/2382898
2、创建模型
D:\mysite\polls\models.py
from django.db import modelsclass Publisher(models.Model): id = models.AutoField(primary_key=True) # 自增的ID主键 name = models.CharField(max_length=64, null=False)
3、建表
python manage.py makemigrationspython manage.py migrate
4、url配置
主项目url
D:\mysite\mysite\urls.py
from django.contrib import adminfrom django.urls import path,includeurlpatterns = [ path('polls/',include('polls.urls')), path('admin/', admin.site.urls),]
应用项目url
D:\mysite\polls\urls.py
from django.urls import pathfrom . import views #添加命名空间app_name = 'polls'urlpatterns = [ #访问列表页 path('publisher_list/', views.published_list,name='published_list'), #添加数据 path('add_publisher/', views.add_publisher,name='add_publisher'), #删除数据 path('delete_publisher/', views.delete_publisher,name='delete_publisher'), #path('edit_publisher/', views.edit_publisher,name='edit_publisher'),]
五、静态html(以后有列表页,数据增、改页面,删除不需要单独html页面)
列表页
D:\mysite\polls\templates\polls\publisher_list.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>出版社列表</title></head><body><a href="/polls/add_publisher/">添加新的出版社</a><table border="1"> <thead> <tr> <th>序号</th> <th>ID</th> <th>出版社名称</th> <th>操作</th> </tr> </thead> <tbody> {% for publisher in publisher_list %} <tr> <td>{{ forloop.counter }}</td> <td>{{ publisher.id }}</td> <td>{{ publisher.name }}</td> <td> <a href="/delete_publisher/?id={{ publisher.id }}">删除</a> <a href="/edit_publisher/?id={{ publisher.id }}">编辑</a> </td> </tr> {% endfor %} </tbody></table></body></html>
#静态htm数据增页面
D:\mysite\polls\templates\polls\add_publisher.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>添加出版社</title></head><body><h2>添加出版社</h2><!--url指提交的数据交给add_publisher函数处理,polls为命名空间名字,绑定了add_publisher函数,指执行polls应用下的add_publisher函数--><form action="{% url 'polls:add_publisher' %}" method="post"> {% csrf_token %} <input type="text" name="publisher_name"> <input type="submit" value="提交"> <p >{{ error }}</p></form></body></html>
#静态html改页面
D:\mysite\polls\templates\polls\edit_publisher.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>编辑出版社</title></head><body><h2>编辑出版社</h2><form action="/polls/edit_publisher/" method="post"> <input type="text" name="id" value="{{ publisher.id }}" > <input type="text" name="publisher_name" value="{{ publisher.name }}"> <input type="submit" value="提交"></form></body></html>
python后端
D:\mysite\polls\views.py
from django.shortcuts import HttpResponse, render, redirectfrom polls import modelsfrom .models import Publisher#列表页函数def published_list(request): ret = Publisher.objects.all().order_by("id") return render(request,"polls/publisher_list.html",{"publisher_list": ret})#添加函数def add_publisher(request): error_msg = "" #如果是POST请求,我就取到用户填写的数据 print(request.method) if request.method == "POST": new_name = request.POST.get("publisher_name", None) if new_name: # 通过ORM去数据库里新建一条记录 Publisher.objects.create(name=new_name) #返回访问列表面,退出 return redirect("/polls/publisher_list/") else: #如果用户post后没有数据则设置变量 error_msg = "出版社名字不能为空!" #如果是get请求访问此页面 return render(request, "polls/add_publisher.html", {"error": error_msg})#删除函数def delete_publisher(request): print(request.GET) print("=" * 120) #1. 从GET请求的参数里面拿到将要删除的数据的ID值 del_id = request.GET.get("id", None) # 字典取值,娶不到默认为None #如果能取到id值 if del_id: # 去数据库删除当前id值的数据 #根据id值查找到数据 del_obj = models.Publisher.objects.get(id=del_id) #删除 del_obj.delete() #返回删除后的页面,跳转到出版社的列表页,查看删除是否成功 return redirect("/polls/publisher_list/") else: return HttpResponse("要删除的数据不存在!")#编辑函数def edit_publisher(request): #用户修改完出版社的名字,点击提交按钮,给我发来新的出版社名字 if request.method == "POST": print(request.POST) #取新出版社名字 edit_id = request.POST.get("id") new_name = request.POST.get("publisher_name") #更新出版社 #根据id取到编辑的是哪个出版社 edit_publisher = models.Publisher.objects.get(id=edit_id) edit_publisher.name = new_name edit_publisher.save() # 把修改提交到数据库 #跳转出版社列表页,查看是否修改成功 return redirect("/polls/publisher_list/") #从GET请求的URL中取到id参数 edit_id = request.GET.get("id") if edit_id: #获取到当前编辑的出版社对象 publisher_obj = models.Publisher.objects.get(id=edit_id) return render(request, "polls/edit_publisher.html", {"publisher": publisher_obj}) else: return HttpResponse("编辑的出版社不存在!")
访问列表页
http://127.0.0.1:8000/polls/publisher_list/
访问添加页
http://127.0.0.1:8000/polls/add_publisher/
访问编辑页
http://127.0.0.1:8000/polls/edit_publisher/?id=7
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。