书的查,增,删,改
阅读目录:
App中的models
from django.db import models # 出版社 class Publisher(models.Model): pid = models.AutoField(primary_key=True) # 主键 name = models.CharField(max_length=32, unique=True) # varchar(32) def __str__(self): return self.name # 书籍 class Book(models.Model): name = models.CharField(max_length=32, unique=True) # varchar(32) pub = models.ForeignKey('Publisher', on_delete=models.CASCADE) 外键对应的表, 级联删除 on_delete=models.CASCADE
urls
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls),# 展示书籍 url(r'^book/', views.book), # 添加书籍 url(r'^add_book/', views.add_book), # 删除书籍 url(r'^del_book/', views.del_book), # 编辑书籍 url(r'^edit_book/', views.edit_book),
App中的views 及templates
查操作
views
# 展示书籍 def book(request): # 获取书籍信息 all_book = models.Book.objects.all() # for book in all_book: # # print(book) # # print(book.pk) # # print(book.name) # # print(book.pub, type(book.pub)) #此处是一个出版的对象 # # print(book.pub.name) #一个出版社对象的名字 # print(book.pub.pk) #这是一个出版社对象的ID # print(book.pub_id) #这也是一个出版社对象的ID
# print('*' * 20) return render(request, 'book.html', {"books": all_book})
templates
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <thead> <tr> <th>序号</th> <th>id</th> <th>书名</th> <th>出版社</th> <th>操作</th> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ forloop.counter }}</td> <td>{{ book.pk }}</td> <td>{{ book.name }}</td> <td>{{ book.pub.name }}</td> #此处相当于是出版社的对象的name属性 <td> <a href="/del_book/?pk={{ book.pk }}">删除</a> <a href="/edit_book/?pk={{ book.pk }}">编辑</a> </td> </tr> {% endfor %} </tbody> </table> </body> </html>
增操作
views
# 添加书籍 def add_book(request): if request.method == 'POST': # 获取提交的数据 new_name = request.POST.get('new_name') pub_id = request.POST.get('pub_id') # 插入到数据库中 # pub_obj = models.Publisher.objects.get(pk=pub_id) # models.Book.objects.create(name=new_name, pub=pub_obj) #对象= 对象 models.Book.objects.create(name=new_name, pub_id=pub_id) #ID之间的相等 # 跳转到展示页面 return redirect('/book/') # 查询所有的出版社信息 all_pubs = models.Publisher.objects.all() return render(request, 'add_book.html', {'all_pubs': all_pubs})
templates
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> <p> 书名:<input type="text" name="new_name"> </p> <p> 出版社: <select name="pub_id"> {% for pub in all_pubs %} <option value="{{ pub.pk }}">{{ pub.name }}</option> {% endfor %} </select> </p> <button>提交</button> </form> </body> </html>
删操作
views
# 删除数据 def del_book(request): # 获取要删除对象的id del_id = request.GET.get('pk') # 数据库删除 models.Book.objects.filter(pk=del_id).delete() return redirect('/book/')
改操作
views
# 编辑书籍 def edit_book(request): # 获取要修改的对象 edit_id = request.GET.get('pk') obj = models.Book.objects.get(pk=edit_id) if request.method == 'POST': # 编辑 new_name = request.POST.get('new_name') pub_id = request.POST.get('pub_id') obj.name = new_name obj.pub_id = pub_id # obj.pub = models.Publisher.objects.get(pk=pub_id) obj.save() return redirect('/book/') # 获取所有的出版社对象 all_pubs = models.Publisher.objects.all() return render(request, 'edit_book.html', {'obj': obj, 'all_pubs': all_pubs})
templates
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> <p> 书名:<input type="text" name="new_name" value="{{ obj.name }}"> </p> <p> 出版社: <select name="pub_id"> {% for pub in all_pubs %} {% if obj.pub == pub %} <option selected value="{{ pub.pk }}">{{ pub.name }}</option> {% else %} <option value="{{ pub.pk }}">{{ pub.name }}</option> {% endif %} {% endfor %} </select> </p> <button>提交</button> </form> </body> </html>