本文实例讲述了django框架使用模板。分享给大家供大家参考,具体如下:
models.py:
from django.db import models # Create your models here. class Book(models.Model): title=models.CharField(max_length=32,unique=True) price=models.DecimalField(max_digits=8,decimal_places=2,null=True) pub_date=models.DateField() publish=models.CharField(max_length=32) is_pub=models.BooleanField(default=True) authors=models.ManyToManyField(to="Author") class AuthorDetail(models.Model): gf=models.CharField(max_length=32) tel=models.CharField(max_length=32) class Author(models.Model): name=models.CharField(max_length=32) age=models.IntegerField() # 与AuthorDetail建立一对一的关系 # ad=models.ForeignKey(to="AuthorDetail",to_field="id",on_delete=models.CASCADE,unique=True) #OneToOneField 表示创建一对一关系。on_delete=models.CASCADE 表示级联删除。假设a表删除了一条记录,b表也还会删除对应的记录 ad=models.OneToOneField(to="AuthorDetail",to_field="id",on_delete=models.CASCADE,)
urls.py:
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), # url(r'',views.index),#这一条不能放上面,会造成死循环 url(r'index/$',views.index), url(r'books/add/$',views.add), url(r'books/manage/',views.manage), url(r'books/delete/(?P\d+)',views.delete), url(r'books/modify/(?P \d+)',views.modify), ]
views.py:
from django.shortcuts import render,HttpResponse from app01 import models # Create your views here. def index(request): ret=models.Book.objects.all().exists()#True 和 False if ret: book_list=models.Book.objects.all() return render(request,'index.html',{'book_list':book_list}) else: # hint='' hint='' return HttpResponse(hint) def add(request): if request.method=="POST": title=request.POST.get("title") price=request.POST.get("price") pub_date=request.POST.get("pub_date") publish=request.POST.get("publish") is_pub=request.POST.get("is_pub") #插入一条记录 obj=models.Book.objects.create(title=title,price=price,publish=publish,pub_date=pub_date,is_pub=is_pub) print(obj.title) hint = '' return HttpResponse(hint) return render(request,"add.html") def manage(request): ret=models.Book.objects.all().exists() print(ret) if ret: book_list=models.Book.objects.all() return render(request,"manage.html",{"book_list":book_list}) else: hint='' return HttpResponse(hint) def delete(request,id): ret=models.Book.objects.filter(id=id).delete() print('删除记录%s'%ret) if ret[0]: hint='' return HttpResponse(hint) else: hint='' return HttpResponse(hint) def modify(request,id): if request.method=="POST": title=request.POST.get('title') price = request.POST.get("price") pub_date = request.POST.get("pub_date") publish = request.POST.get("publish") is_pub = request.POST.get("is_pub") # 更新一条记录 ret = models.Book.objects.filter(id=id).update(title=title, price=price, publish=publish, pub_date=pub_date, is_pub=is_pub) print('更新记录%s'%ret) if ret: # 判断返回值为1 hint = '' return HttpResponse(hint) # js跳转 else: # 返回为0 hint = '' return HttpResponse(hint) # js跳转 book=models.Book.objects.get(id=id) return render(request,"modify.html",{"book":book})
index.html:
{% extends 'base.html' %} {% block title %}查看书籍 {% endblock title %} {% block content %}查看书籍
名称 | 价格 | 出版日期 | 出版社 | 是否出版 |
---|---|---|---|---|
{{ book.title }} | {{ book.price }} | {{ book.pub_date|date:"Y-m-d" }} | {{ book.publish }} | {% if book.is_pub %} 已出版 {% else %} 未出版 {% endif %} |
add.html:
{% extends 'base.html' %} {% block title %}{% endblock content %}添加书籍 {% endblock title %} {% block content %}添加书籍
base.html:
{% block title %}Title {% endblock title %}书籍操作
manage.py:
{% extends 'base.html' %} {% block title %}管理书籍 {% endblock title %} {% block content %}管理书籍
modify.py:
{% extends 'base.html' %} {% block title %}{% endblock content %}修改书籍 {% endblock title %} {% block content %}修改书籍
django使用一对多和多对多关系建表之后的增删改查
-------models.py-----
from django.db import models # Create your models here. class Book(models.Model): title=models.CharField(max_length=32) price=models.DecimalField(max_digits=6,decimal_places=2) create_time=models.DateField() memo=models.CharField(max_length=32,default="") publish=models.ForeignKey(to="Publish",default=1) author=models.ManyToManyField("Author")#on_delete=models.CASCADE()默认级联删除 def __str__(self): return self.title class Publish(models.Model): name=models.CharField(max_length=32) email=models.CharField(max_length=32) class Author(models.Model): name=models.CharField(max_length=32) def __str__(self): return self.name
-----urls.py----
from django.conf.urls import url,include from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^books/$',views.books), #查看 url(r'^addbook/$',views.addbook), #增加 url(r'^delbook/(\d+)/$',views.delbook), #删除 url(r'editbook/(\d+)/$',views.editbook), #修改 ]
----views.py 在app01下面-------
from django.shortcuts import render,HttpResponse,redirect from .models import * def books(request): book_list=Book.objects.all() return render(request,"books.html",locals()) def addbook(request): if request.method=="POST": title=request.POST.get("title") #get方法取的是html页面中的name属性 price= request.POST.get("price") date = request.POST.get("date") publish_id=request.POST.get("publish_id") # author_id_list = request.POST.get("author_id_list") #此方法只能取到最后一个值 author_id_list = request.POST.getlist("author_id_list") #有多个值的注意要用getlist #绑定书籍与出版社一对多的关系 obj=Book.objects.create(title=title,price=price,create_time=date,publish_id=publish_id) #绑定书籍与作者多对多的关系 obj.author.add(*author_id_list) # obj.author.remove(1,2) #解除关系 # obj.author.clear() #清空所有关系 return redirect("/books/") else: publish_list=Publish.objects.all() author_list=Author.objects.all() return render(request,"addbook.html",locals()) def delbook(request,id): Book.objects.filter(id=id).delete() return redirect("/books/") def editbook(request,id): if request.method=="POST": title=request.POST.get("title") #get方法取的是html页面中的name属性 price=request.POST.get("price") date=request.POST.get("date") publish_id=request.POST.get("publish_id") author_id_list=request.POST.getlist("author_id_list") Book.objects.filter(id=id).update(title=title,price=price,create_time=date,publish_id=publish_id) book=Book.objects.filter(id=id).first() # book.author.clear() # book.author.add(*author_id_list) book.author.set(author_id_list) #相当于上面两条 return redirect ("/books/") edit_obj=Book.objects.filter(id=id).first() #加first从queryset得到 models对象 publish_list = Publish.objects.all() author_list = Author.objects.all() return render(request,"editbook.html",locals())
----books.html----
Title
-----addbook.html-----
Title
-------editbook.html-----
Title
希望本文所述对大家基于Django框架的Python程序设计有所帮助。