demo Django-基础书籍添加删除(单表)

时间:2019-09-04
本文章向大家介绍demo Django-基础书籍添加删除(单表),主要包括demo Django-基础书籍添加删除(单表)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

小demo使用----

1.pycharm-2019.2

2.python-3.7.2

3.mysql-5.7.25

4.django-2.2.4

使用过程中的一些注意事项和出现的常见错误的解决地址

前端实例实现效果

添加书籍

?

?查看书籍

?

?删除书籍

?

?

?编辑书籍

?

?

?

?

?

?

?

部分代码:

部分设置,以及文件分布

?

?数据库提前建好的情况下,建表格

 1 from django.db import models
 2 
 3 # Create your models here.
 4 
 5 
 6 class Book(models.Model):
 7     id = models.AutoField(primary_key=True)
 8     title = models.CharField(max_length=32)
 9     pub_date = models.DateField()
10     price = models.DecimalField(max_digits=8, decimal_places=2)
11     publish = models.CharField(max_length=32)
models.py

控制器

 1 """bookms URL Configuration
 2 
 3 The `urlpatterns` list routes URLs to views. For more information please see:
 4     https://docs.djangoproject.com/en/2.2/topics/http/urls/
 5 Examples:
 6 Function views
 7     1. Add an import:  from my_app import views
 8     2. Add a URL to urlpatterns:  path('', views.home, name='home')
 9 Class-based views
10     1. Add an import:  from other_app.views import Home
11     2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
12 Including another URLconf
13     1. Import the include() function: from django.urls import include, path
14     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
15 """
16 from django.contrib import admin
17 from django.urls import path, re_path
18 from app01 import views
19 
20 urlpatterns = [
21     path('admin/', admin.site.urls),
22     path('addbook/', views.addbook),
23     path('books/', views.books),
24     re_path(r"books/(\d+)/delete", views.delbook),
25     re_path(r"books/(\d+)/change", views.changebook),
26 ]
urls.py

视图

 1 from django.shortcuts import render, HttpResponse, redirect
 2 
 3 # Create your views here.
 4 
 5 from app01.models import Book
 6 
 7 def addbook(request):
 8     if request.method == "POST":
 9 
10         title = request.POST.get("title")
11         price = request.POST.get("price")
12         date = request.POST.get("date")
13         publish = request.POST.get("publish")
14 
15         book_obj = Book.objects.create(title=title, price=price, pub_date=date, publish=publish)
16         return redirect("/books/")
17 
18     return render(request, "addbook.html")
19 
20 def books(request):
21     book_list = Book.objects.all()
22 
23     return render(request, "books.html", locals())
24 
25 def delbook(request, id):
26 
27     Book.objects.filter(id=id).delete()
28 
29     return redirect("/books/")
30 
31 def changebook(request, id):
32     book_obj = Book.objects.filter(id=id).first()
33 
34     if request.method == "POST":
35         title = request.POST.get("title")
36         price = request.POST.get("price")
37         date = request.POST.get("date")
38         publish = request.POST.get("publish")
39         Book.objects.filter(id=id).update(title=title, price=price, pub_date=date, publish=publish)
40         return redirect("/books/")
41 
42     return render(request, "changebook.html", {'book_obj': book_obj})
views.py
 1 
 2 "en">
 3 
 4     "UTF-8">
 5     Title
 6     "stylesheet" href="/static/bs/css/bootstrap.css">
 7     
15 
16 
17 
18     

查看书籍

19 20
class="container"> 21
class="row"> 22
class="col-md-6 col-md-offset-3"> 23 "/addbook" class="btn btn-primary">添加书籍 24
class="table table-striped table-bordered"> 25 26 27 书籍名称 28 价格 29 出版日期 30 出版社 31 删除操作 32 编辑操作 33 34 35 36 {% for book in book_list %} 37 38 {{ book.title }} 39 {{ book.price }} 40 {{ book.pub_date|date:'Y-m-d' }} 41 {{ book.publish }} 42 "/books/{{ book.pk }}/delete" class="btn btn-danger">删除 43 "/books/{{ book.pk }}/change" class="btn btn-info">编辑 44 45 {% endfor %} 46 47 48 49
50
51
52