django框架两个使用模板实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django框架两个使用模板实例相关的知识,希望对你有一定的参考价值。
前言
这篇文章主要介绍了django框架使用模板方法,结合两个具体实例形式详细分析了Django框架模板的相关使用技巧与操作注意事项,需要的朋友可以参考下。
我在这里做一次集中式分享,如果有喜欢的内容,尽管拿去,如果遇到什么问题,请在下面给我留言即可!
本文实例讲述了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<id>\d+)‘,views.delete), url(r‘books/modify/(?P<id>\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=‘<script>alert("没有书籍,请添加书籍");window.location.href="/books/add" rel="external nofollow" rel="external nofollow" </script>‘ hint=‘<script>alert("没有书籍,请添加书籍");window.location.href="/books/add/" rel="external nofollow" </script>‘ 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 = ‘<script>alert("添加成功");window.location.href="/index/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" </script>‘ 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=‘<script>alert("没有书籍,请添加书籍");window.location.href="/books/add" rel="external nofollow" rel="external nofollow" </script>‘ return HttpResponse(hint) def delete(request,id): ret=models.Book.objects.filter(id=id).delete() print(‘删除记录%s‘%ret) if ret[0]: hint=‘<script>alert("删除成功");window.location.href="/index/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" </script>‘ return HttpResponse(hint) else: hint=‘<script>alert("删除失败");window.location.href="/index/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" </script>‘ 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 = ‘<script>alert("修改成功");window.location.href="/index/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" </script>‘ return HttpResponse(hint) # js跳转 else: # 返回为0 hint = ‘<script>alert("修改失败");window.location.href="/index/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" </script>‘ return HttpResponse(hint) # js跳转 book=models.Book.objects.get(id=id) return render(request,"modify.html",{"book":book})
以上是关于django框架两个使用模板实例的主要内容,如果未能解决你的问题,请参考以下文章