1.外键相关
一对多的概念 ,这里是一个出版社对应本书籍 ! 设计表使用model
models.ForeignKey('关联一', on_delete=models.CASCADE) #给多设置外键 ,关联一 ,设置级联删除当一的内容被删除 ,所关联的多都被删除
from django.db import models # Create your models here. class presslist(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name class Book(models.Model): name = models.CharField(max_length=32) p_id = models.ForeignKey('presslist', on_delete=models.CASCADE)
2.urls.py代码
两套增删改查
from django.conf.urls import url from app1 import views urlpatterns = [ url(r'^press/list/', views.presslist, name='presslist'), url(r'^press/del/(d+)/', views.pressdel, name='pressdel'), url(r'^press/add/', views.pressadd, name='pressadd'), url(r'^press/edit/(?P<edit_id>d+)/', views.pressedit1.as_view(), name='pressedit'), url(r'^book/list/', views.booklist, name='booklist'), url(r'^book/del/(d+)/', views.bookdel, name='bookdel'), url(r'^book/add/', views.bookadd, name='bookadd'), url(r'^book/edit/(d+)/', views.bookedit, name='bookedit'), ]
3.views视图函数
两套视图函数
重点的 :models.Book.objects.create(name=book_name, p_id_id=press_id) ,其中创建书籍记录需要关联出版社 ,这里面必须是从出版社表取数据p_id_id
from django.shortcuts import render, redirect, reverse, HttpResponse from django.views import View from app1 import models import time import functools from django.utils.decorators import method_decorator # Create your views here. def presslist(request): msg_all = models.presslist.objects.all() return render(request, 'cbs.html', {'msg': msg_all}) def pressadd(request): if request.method == 'POST': press_name = request.POST.get('name') models.presslist.objects.create(name=press_name) return redirect(reverse('presslist')) return render(request, 'cbsadd-edit.html') def pressdel(request, del_id): print(del_id, type(del_id)) models.presslist.objects.get(pk=del_id).delete() return redirect(reverse('presslist')) def pressedit(request, edit_id, test_id): obj = models.presslist.objects.get(pk=edit_id) print(test_id, edit_id) msg = '' if request.method == 'POST': obj.name = request.POST.get('name') if models.presslist.objects.filter(name=obj.name): msg = '已存在' if not obj.name: msg = '不能为空' if not models.presslist.objects.filter(name=obj.name) and obj.name: obj.save() return redirect(reverse('presslist')) return render(request, 'cbsadd-edit.html', {'obj': obj, 'msg': msg}) class pressedit1(View): def get(self, request, edit_id, msg=''): obj = models.presslist.objects.get(pk=edit_id) return render(request, 'cbsadd-edit.html', {'obj': obj, 'msg': msg}) def post(self, request, edit_id): msg = '' obj = models.presslist.objects.get(pk=edit_id) obj.name = request.POST.get('name') if models.presslist.objects.filter(name=obj.name): msg = '已存在' if not obj.name: msg = '不能为空' if not models.presslist.objects.filter(name=obj.name) and obj.name: obj.save() return redirect(reverse('presslist')) return self.get(request, edit_id, msg) def booklist(request): msg_all = models.Book.objects.all() return render(request, 'cbs.html', {'msg': msg_all}) def bookdel(request, del_id): models.Book.objects.get(pk=del_id).delete() return redirect(reverse('booklist')) def bookadd(request): msg_all = models.presslist.objects.all() if request.method == 'POST': book_name = request.POST.get('name') press_id = int(request.POST.get('pid')) models.Book.objects.create(name=book_name, p_id_id=press_id) return redirect(reverse('booklist')) return render(request, 'cbsadd-edit.html', {'msg_all': msg_all}) def bookedit(request, edit_id): obj = models.Book.objects.get(pk=edit_id) msg = '' if request.method == 'POST': obj.name = request.POST.get('name') if models.Book.objects.filter(name=obj.name): msg = '已存在' if not obj.name: msg = '不能为空' if not models.Book.objects.filter(name=obj.name) and obj.name: obj.save() return redirect(reverse('booklist')) return render(request, 'cbsadd-edit.html', {'obj': obj, 'msg': msg})
4.模板复用
cbs.html
###展示出版社 ,展示书籍列表的模板 {% extends 'base.html' %} {% block body1 %} <table class="text-center table table-striped table-bordered"> <tr> <td>ID</td> {% if msg.0.p_id %} <td>书名</td> <td>出版社名字</td> {% else %} <td>出版社名字</td> {% endif %} <td>操作</td> </tr> {% for obj in msg %} <tr> <td>{{ obj.pk }}</td> <td>{{ obj.name }}</td> {% if obj.p_id %} <td>{{ obj.p_id.name }}</td> <td><a href={% url 'bookedit' obj.pk %}><i class="fa fa-pencil-square-o" aria-hidden="true">   </i></a> <a href={% url 'bookdel' obj.pk %}><i class="fa fa-remove" aria-hidden="true"></i></a></td> {% else %} <td><a href={% url 'pressedit' edit_id=obj.pk %}><i class="fa fa-pencil-square-o" aria-hidden="true">   </i></a> <a href={% url 'pressdel' obj.pk %}><i class="fa fa-remove" aria-hidden="true"></i></a></td> {% endif %} </tr> {% endfor %} </table> {% endblock %}
cbsadd-edit.html
###修改与新增的复用模板 {% extends 'base.html' %} {% block body1 %} <form action="" method="post"> <div style="margin-left: 100px;margin-top: 50px;" class="col-md-6 "> <input type="text" class="form-control" name="name" placeholder="{{ obj.name }}">{{ msg }} {% if msg_all %} <select name="pid"> {% for obj in msg_all %} <option value="{{ obj.pk }}">{{ obj.name }}</option> {% endfor %} </select> {% endif %} </div> <div style="margin-left: 10px;margin-top: 50px;" class="col-md-2 "> <button type="submit" class="btn btn-primary">提交</button> </div> </form> {% endblock %}