demo Django- books Add Remove basis (single table)

Use a small demo ----

1.pycharm-2019.2

2.python-3.7.2

3.mysql-5.7.25

4.django-2.2.4

Use some of the considerations and common errors to solve the address that appears

Examples of results achieved distal

Add books

 

 View books

 

 Delete books

 

 

 Edited books

 

 

 

 

 

 

 

Part of the code:

Part of the set, and file distribution

 

 If the database built in advance, built form

 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

Controller

 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

view

 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 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
 7     <style>
 8         .container{
 9             margin-top: 100px;
10         }
11         .btn{
12             margin-top: 10px;
13         }
14     </style>
15 </head>
16 <body>
17 
18     <h3>查看书籍</h3>
19 
20     <div class="container">
21         <div class="row">
22             <div class="col-md-6 col-md-offset-3">
23                 <a href="/addbook" class="btn btn-primary"> Add Books </a>
 24                  <Table class = " Table Table Table-Striped-bordered " >
 25                      <thead>
 26 is                      <TR>
 27                          <TH> Name Books </ TH>
 28                          <TH> Price </ TH>
 29                          <th> published </ TH>
 30                          <th> press </ TH>
 31 is                          <th> delete </ TH>
 32                          <th> editing </ TH>
 33 is                      </ TR>
34                     </thead>
35                     <tbody>
36                         {% for book in book_list %}
37                             <tr>
38                                 <td>{{ book.title }}</td>
39                                 <td>{{ book.price }}</td>
40                                 <td>{{ book.pub_date|date:'Y-m-d' }}</td>
41                                 <td>{{ book.publish }}</td>
42                                 <td><a href="/books/{{ book.pk }}/delete" class="btn btn-danger">删除</a></td>
43                                 <td><a href="/books/{{ book.pk }}/change" class="btn btn-info">编辑</a></td>
44                             </tr>
45                         {% endfor %}
46                         
47                     </tbody>
48                 </table>
49             </div>
50         </div>
51     </div>
52 </body>
53 </html>
books.html
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
 7     <style>
 8         .container{
 9             margin-top: 100px;
10         }
11         .btn{
12             margin-top: 10px;
13         }
14     </style>
15 </head>
16 <body>
17 
18     <h3>添加书籍</h3>
19 
20     <div class="container">
21         <div class="row">
22             <div class="col-md-6 col-md-offset-3">
23                 <a href="/books" class="btn btn-primary">查看书籍</a>
24                 <form action="" method="post">
25                     {% csrf_token %}
26                     <div>
27                         <label for="">书籍名称</label>
28                         <input type="text" class="form-control" name="title">
29                     </div>
30                     <div>
31                         <label for="">价格</label>
32                         <input type="text" class="form-control" name="price">
33                     </div>
34                     <div>
35                         <label for="">出版日期</label>
36                         <input type="date" class="form-control" name="date">
37                     </div>
38                     <div>
39                         <label for="">出版社</label>
40                         <input type="text" class="form-control" name="publish">
41                     </div>
42 
43                     <input type="submit"  class="btn btn-success pull-right">
44                 </form>
45             </div>
46         </div>
47     </div>
48 </body>
49 </html>
addbook.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <style>
        .container{
            margin-top: 100px;
        }
        .btn{
            margin-top: 10px;
        }
    </style>
</head>
<body>

    <h3>Edited books </ h3> =class

    <div"container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <a href="/books" class="btn btn-primary">查看书籍</a>
                <form action="" method="post">
                    {% csrf_token %}
                    <div>
                        <label for="">书籍名称</label>
                        <input type="text" class="form-control" name="title" value="{{ book_obj.title }}">
                    </div>
                    <div>
                        <label for="">价格</label>
                        <input type="text" class="form-control" name="price" value="{{ book_obj.price }}">
                    </div>
                    <div>
                        <label for="">出版日期</label>
                        <input type="date" class="form-control" name="date" value="{{ book_obj.pub_date|date:'Y-m-d' }}">
                    </div>
                    <div>
                        <label for=""
                        <INPUT type => Publishing </ label>"text" class="form-control" name="publish" value="{{ book_obj.publish }}">
                    </div>

                    <input type="submit"  class="btn btn-success pull-right">
                </form>
            </div>
        </div>
    </div>
</body>
</html>
changebook.html

 

Guess you like

Origin www.cnblogs.com/Xanderzyl/p/11461793.html