图书管理系统作业

这个作业是刚学django做的,一直没再看,所以现在才发,很多功能都没有

整个项目需要注意的点:

有空再补充(这样说的一般后来都会忘记,尴尬😅)

1.开启一个项目第一件事就是去settings看看数据库配置和静态路由,然后再检查一下各个路径配置和app有没有写进去。static需要手动去创建。

2.然后去models把表创建好,关联好,makemigrations做下迁移记录,migrate刷进数据库。如果有多对多可以去数据库查看下有没有自动帮忙创建。

3.在urls开好口子,方便浏览器访问,然后就可以去写视图函数和对应的模板。

4.视图函数注意点很多,以下一一列举:

4.1 首先要导入models ==》 from app01.models import * 

4.2 定义的函数形参默认是request:request常用三个属性:method 获得请求的方式POST或者GET,也可以用于做判断。

request.POST  获得通过post请求方式发过来的请求体,可以用get对其里面属性进行取值。

request.GET获得通过GET请求方式发过来的请求

 


 

 

需要注意的几个点:

0.先配置好settings,数据库配置,习惯用mysql,配置好路径,配置好静态路由,csrf文件先注释掉,还有一个要注意的就是如果app不是在创建项目创建之时,

后面手动创建的app需要自己加到installed_apps.

1.先建立好他们之间的关系:作者和书多对多,书和出版社多对一

所以,我们先按照这个关系建好表,如下:

 1 from django.db import models
 2 
 3 # Create your models here.
 4 class Publish(models.Model):
 5     # 可以不写,默认会帮你创建
 6     nid=models.AutoField(primary_key=True)
 7     name=models.CharField(max_length=32)
 8     addr=models.CharField(max_length=64)
 9     # 也是varchar类型
10     email=models.EmailField()
11 
12 class Author(models.Model):
13     nid = models.AutoField(primary_key=True)
14     name=models.CharField(max_length=32)
15     age=models.IntegerField()
16 
17 class Book(models.Model):
18     nid = models.AutoField(primary_key=True)
19     name=models.CharField(max_length=32)
20     price=models.DecimalField(max_digits=5,decimal_places=2)
21     pub_date=models.DateField()
22     # to那个表,可以不加引号,但是,要在前面定义
23     publish=models.ForeignKey(to='Publish',to_field='nid')
24     authors=models.ManyToManyField(to='Author')
25 
26 
27 # 等同于authors=models.ManyToManyField(to='Author')
28 # 不需要额外再建一张表
29 # class Book2Author(models.Model):
30 #     nid=models.AutoField(primary_key=True)
31 #     book=models.ForeignKey(to='Book',to_field='nid')
32 #     author=models.ForeignKey(to='Author',to_field='nid')
在models里建好数据库中的表

 

2.开好路由,外界访问时候先通过路由映射关系找到函数,记得导入视图模块

 1 """untitled URL Configuration
 2 
 3 The `urlpatterns` list routes URLs to views. For more information please see:
 4     https://docs.djangoproject.com/en/1.11/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:  url(r'^$', 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:  url(r'^$', Home.as_view(), name='home')
12 Including another URLconf
13     1. Import the include() function: from django.conf.urls import url, include
14     2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
15 """
16 from django.conf.urls import url
17 from django.contrib import admin
18 from app01 import views
19 urlpatterns = [
20     url(r'^admin/', admin.site.urls),
21     url(r'^add_publish/', views.add_publish),
22     url(r'^publish_list/', views.publish_list),
23     url(r'^delete_publish/', views.delete_publish),
24     url(r'^edit_publish/', views.edit_publish),
25     url(r'^add_book/', views.add_book),
26     url(r'^book_list/', views.book_list),
27     url(r'^delete_book/', views.delete_book),
28     url(r'^edit_book/',views.edit_book),
29     url(r'^add_author/', views.add_author),
30     url(r'^author_list/', views.author_list),
31     url(r'^delete_author/', views.delete_author),
32     url(r'^edit_author/',views.edit_author),
33 ]
路由

 

3.写视图函数

  1 from django.shortcuts import render, HttpResponse, redirect
  2 # from app01 import models
  3 from app01.models import *
  4 
  5 
  6 # Create your views here.
  7 
  8 
  9 def add_publish(request):
 10     if request.method == 'POST':
 11         name = request.POST.get('name')
 12         addr = request.POST.get('addr')
 13         email = request.POST.get('email')
 14         Publish.objects.create(name=name, addr=addr, email=email)
 15         return redirect('/publish_list/')
 16 
 17     return render(request, 'add_publish.html')
 18 
 19 
 20 def publish_list(request):
 21     publish_list = Publish.objects.all()
 22     return render(request, 'publish_list.html', {'publish_list': publish_list})
 23 
 24 
 25 def delete_publish(request):
 26     nid = request.GET.get('nid')
 27     print(nid)
 28     # 通过fileter过滤出nid为传入的nid,删除掉
 29     # 过滤出nid=2的数据
 30     ret = Publish.objects.filter(nid=nid)
 31     # 删除这些数据
 32     ret.delete()
 33     return redirect('/publish_list/')
 34 
 35 
 36 def edit_publish(request):
 37     if request.method == 'POST':
 38         nid = request.POST.get('nid')
 39         name = request.POST.get('name')
 40         addr = request.POST.get('addr')
 41         email = request.POST.get('email')
 42         Publish.objects.filter(pk=nid).update(name=name, addr=addr, email=email)
 43         return redirect('/publish_list/')
 44     # 这样写的话有点局限性,必须要先获得nid
 45     nid = request.GET.get('nid')
 46     publish = Publish.objects.filter(nid=nid).first()
 47     return render(request, 'edit_publish.html', {'publish': publish})
 48 
 49 
 50 def add_book(request):
 51     if request.method == 'POST':
 52         name = request.POST.get('name')
 53         price = request.POST.get('price')
 54         pub_date = request.POST.get('pub_date')
 55         publish_id = request.POST.get('publish')
 56         authors = request.POST.getlist('authors')
 57         book = Book.objects.create(name=name, price=price, pub_date=pub_date, publish_id=publish_id)
 58         # for i in authors:
 59         #     book.authors.add(i)
 60         # 它会去中间表(book_authors),添加记录
 61         book.authors.add(*authors)
 62 
 63         return redirect('/book_list/')
 64     publish_list = Publish.objects.all()
 65     author_list = Author.objects.all()
 66     return render(request, 'add_book.html', {'publish_list': publish_list, 'author_list': author_list})
 67 
 68 
 69 def book_list(request):
 70     book_list = Book.objects.all()
 71 
 72     return render(request, 'book_list.html', {'book_list': book_list})
 73 
 74 
 75 def delete_book(request):
 76     nid = request.GET.get('nid')
 77     Book.objects.filter(pk=nid).delete()
 78     return redirect('/book_list/')
 79 
 80 
 81 def edit_book(request):
 82     if request.method == 'POST':
 83         nid = request.POST.get('nid')
 84         name = request.POST.get('name')
 85         price = request.POST.get('price')
 86         pub_date = request.POST.get('pub_date')
 87         publish_id = request.POST.get('publish')
 88         authors = request.POST.getlist('authors')
 89         Book.objects.filter(pk=nid).update(name=name, price=price, pub_date=pub_date, publish_id=publish_id)
 90         book = Book.objects.filter(pk=nid).first()
 91         book.authors.set(authors)
 92 
 93         return redirect('/book_list/')
 94     nid = request.GET.get('nid')
 95     book = Book.objects.filter(pk=nid).first()
 96     author_list = Author.objects.all()
 97     publish_list = Publish.objects.all()
 98     return render(request,'edit_book.html',{'book':book,'publish_list':publish_list,'author_list':author_list})
 99 
100 
101 def author_list(request):
102     author_list = Author.objects.all()
103     return render(request, 'author_list.html', {'author_list': author_list})
104 
105 
106 def add_author(request):
107     if request.method == 'POST':
108         name = request.POST.get('name')
109         age = request.POST.get('age')
110         Author.objects.create(name=name, age=age)
111         return redirect('/author_list/')
112 
113     return render(request, 'add_author.html')
114 
115 
116 def delete_author(request):
117     nid = request.GET.get('nid')
118     Author.objects.filter(pk=nid).delete()
119     return redirect('/author_list/')
120 
121 
122 def edit_author(request):
123     if request.method == 'POST':
124         nid = request.POST.get('nid')
125         name = request.POST.get('name')
126         age = request.POST.get('age')
127 
128         Author.objects.filter(pk=nid).update(name=name, age=age)
129         return redirect('/author_list/')
130     nid = request.GET.get('nid')
131     author = Author.objects.filter(pk=nid).first()
132     return render(request, 'edit_author.html', {'author': author})
视图函数

 

4.视图函数需要调用模板,并且有的需要调用数据库中的数据来对模板进行渲染

下面是本次用到的9个模板

作者相关联的表

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>作者列表</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9 <div class="container">
10     <div class="row">
11         <div class="col-md-6 col-md-offset-3">
12             <h2>出版社列表</h2>
13             <table class="table table-condensed">
14                 <thead>
15                 <tr>
16                     <th>ID</th>
17                     <th>作者姓名</th>
18                     <th>作者年龄</th>
19                 </tr>
20                 </thead>
21                 <tbody>
22                     {% for author in author_list %}
23                         <tr>
24                             <td>{{ forloop.counter }}</td>
25                             <td>{{ author.name}}</td>
26                             <td>{{ author.age }}</td>
27                             <td>
28                                 <a href="/delete_author/?nid={{ author.nid }}" class="btn btn-danger">删除</a>
29                                 <a href="/edit_author/?nid={{ author.nid }}" class="btn btn-success">编辑</a>
30                             </td>
31                         </tr>
32                     {% endfor %}
33                 </tbody>
34             </table>
35         </div>
36     </div>
37 </div>
38 </body>
39 </html>
author_list
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>添加作者</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 
 8 </head>
 9 <body>
10 <div class="container">
11     <div class="row">
12         <div class="col-md-6 col-md-offset-3">
13             <h2>添加作者</h2>
14             <form action="/add_author/" method="post">
15                 <p>作者姓名:<input type="text" name="name" class="form-control"></p>
16                 <p>作者年龄:<input type="text" name="age" class="form-control"></p>
17                 <p><input type="submit" value="添加" class="form-control btn btn-success"></p>
18             </form>
19         </div>
20     </div>
21 </div>
22 
23 </body>
24 </html>
add_author
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>修改作者</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9 <div class="container">
10     <div class="row">
11         <div class="col-md-6 col-md-offset-3">
12             <h2>修改作者</h2>
13             <form action="/edit_author/" method="post">
14                 <input type="hidden" name="nid" class="form-control" value="{{ author.nid }}">
15                 <p>作者姓名:<input type="text" name="name" class="form-control" value="{{ author.name }}"></p>
16                 <p>作者年龄:<input type="text" name="age" class="form-control" value="{{ author.age }}"></p>
17                 <p><input type="submit" value="修改" class="form-control btn btn-success"></p>
18 
19 
20             </form>
21             <form action=""></form>
22 
23         </div>
24     </div>
25 </div>
26 </body>
27 </html>
edit_author

 

出版社相关联的表

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 6     <title>出版社列表</title>
 7 </head>
 8 <body>
 9 <div class="container">
10     <div class="row">
11         <div class="col-md-6 col-md-offset-3">
12             <h2>出版社列表</h2>
13             <table class="table table-condensed">
14                 <thead>
15                 <tr>
16                     <th>ID</th>
17                     <th>出版社名称</th>
18                     <th>出版社地址</th>
19                     <th>邮箱</th>
20                     <th>操作</th>
21                 </tr>
22                 </thead>
23                 <tbody>
24                     {% for publish in publish_list %}
25                         <tr>
26                             <td>{{ forloop.counter }}</td>
27                             <td>{{ publish.name}}</td>
28                             <td>{{ publish.addr }}</td>
29                             <td>{{ publish.email }}</td>
30                             <td>
31                                 <a href="/delete_publish/?nid={{ publish.nid }}" class="btn btn-danger">删除</a>
32                                 <a href="/edit_publish/?nid={{ publish.nid }}" class="btn btn-success">编辑</a>
33                             </td>
34                         </tr>
35                     
36                     {% endfor %}
37                     
38                 </tbody>
39             </table>
40 
41 
42         </div>
43     </div>
44 </div>
45 </body>
46 </html>
publish_list
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>新增出版社</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9 
10 <div class="container">
11     <div class="row">
12         <div class="col-md-6 col-md-offset-3">
13             <h2>添加出版社</h2>
14             <form action="/add_publish/" method="post">
15                 <p>出版社名称:<input type="text" name="name" class="form-control"></p>
16                 <p>出版社地址:<input type="text" name="addr" class="form-control"></p>
17                 <p>出版社邮箱:<input type="text" name="email" class="form-control"></p>
18                 <p><input type="submit" value="添加" class="form-control btn btn-success"></p>
19 
20 
21 
22             </form>
23 
24         </div>
25     </div>
26 </div>
27 
28 </body>
29 </html>
add_publish
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>修改出版社</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9 
10 
11 <div class="container">
12     <div class="row">
13         <div class="col-md-6 col-md-offset-3">
14             <h2>修改出版社</h2>
15             <form action="/edit_publish/" method="post">
16                 <input type="hidden" name="nid" class="form-control" value="{{ publish.nid }}">
17                 <p>出版社名称:<input type="text" name="name" class="form-control" value="{{ publish.name }}"></p>
18                 <p>出版社地址:<input type="text" name="addr" class="form-control" value="{{ publish.addr }}"></p>
19                 <p>出版社邮箱:<input type="text" name="email" class="form-control" value="{{ publish.email }}"></p>
20                 <p><input type="submit" value="修改" class="form-control btn btn-success"></p>
21 
22 
23             </form>
24 
25         </div>
26     </div>
27 </div>
28 </body>
29 </html>
edit_publish

 

书籍相关联的表

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>图书列表</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9 <div class="container">
10     <div class="row">
11         <div class="col-md-6 col-md-offset-3">
12             <h2>图书列表</h2>
13             <table class="table table-condensed">
14                 <thead>
15                 <tr>
16                     <th>ID</th>
17                     <th>书名</th>
18                     <th>价格</th>
19                     <th>出版日期</th>
20                     <th>出版社名称</th>
21                     <th>作者</th>
22                     <th>操作</th>
23                 </tr>
24                 </thead>
25                 <tbody>
26                     {% for book in book_list %}
27                         <tr>
28                             <td>{{ forloop.counter }}</td>
29                             <td>{{ book.name}}</td>
30                             <td>{{ book.price }}</td>
31                             <td>{{ book.pub_date|date:'Y-m-d' }}</td>
32                             <td>{{ book.publish.name }}</td>
33                             <td>
34                                 {% for author in  book.authors.all  %}
35                                     {{ author.name }}|
36                                 {% endfor %}
37 
38                             </td>
39 
40                             <td>
41                                 <a href="/delete_book/?nid={{ book.nid }}" class="btn btn-danger">删除</a>
42                                 <a href="/edit_book/?nid={{ book.nid }}" class="btn btn-success">编辑</a>
43                             </td>
44                         </tr>
45 
46                     {% endfor %}
47 
48                 </tbody>
49             </table>
50 
51 
52         </div>
53     </div>
54 </div>
55 
56 
57 </body>
58 </html>
book_list
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>新增图书</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9 <div class="container">
10     <div class="row">
11         <div class="col-md-6 col-md-offset-3">
12             <h2>新增图书</h2>
13             <form action="/add_book/" method="post">
14                 <input type="hidden" name="nid" class="form-control" value="{{ publish.nid }}">
15                 <p>图书名称:<input type="text" name="name" class="form-control"></p>
16                 <p>价格:<input type="text" name="price" class="form-control" ></p>
17                 <p>出版日期:<input type="date" name="pub_date" class="form-control"></p>
18                 <p>出版社:
19                     <select name="publish" id="" class="form-control">
20                         {% for publish in publish_list %}
21                         <option value="{{ publish.nid }}">{{ publish.name }}</option>
22                         {% endfor %}
23                     </select>
24                 </p>
25             <p>作者:
26                 <select name="authors" id="" multiple class="form-control">
27                     {% for author in author_list %}
28                         <option value="{{ author.nid }}">{{ author.name }}</option>
29 
30                     {% endfor %}
31 
32 
33                 </select>
34 
35             </p>
36 
37 
38                 <p><input type="submit" value="提交" class="form-control btn btn-success"></p>
39 
40 
41             </form>
42 
43         </div>
44     </div>
45 </div>
46 </body>
47 </html>
add_book
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>编辑书籍</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9     <div class="container">
10     <div class="row">
11         <div class="col-md-6 col-md-offset-3">
12             <h2>修改书籍</h2>
13             <form action='/edit_book/' method="post">
14                 <input type="hidden" name="nid" class="form-control" value="{{ book.nid }}">
15                 <p>书籍名称:<input type="text" name="name" class="form-control" value="{{ book.name }}"></p>
16                 <p>书籍价格:<input type="text" name="price" class="form-control" value="{{ book.price }}"></p>
17                 <p>出版日期:<input type="date" name="pub_date" class="form-control" value="{{ book.pub_date|date:'Y-m-d'}}"></p>
18                 <p>出版社:
19                     <select name="publish" id="" class="form-control">
20                         {% for publish in publish_list %}
21                             {% if book.publish == publish %}
22                                 <option selected value="{{ publish.nid }}">{{ publish.name }}</option>
23                             {% else %}
24                                 <option value="{{ publish.nid }}">{{ publish.name }}</option>
25                             {% endif %}
26 
27                         {% endfor %}
28                     </select>
29                 </p>
30                  <p>作者:
31                     <select name="authors" id="" multiple class="form-control">
32                         {% for author in author_list %}
33                             {% if author in book.authors.all %}
34                                 <option selected value="{{ author.nid }}">{{ author.name }}</option>
35                             {% else %}
36                                 <option value="{{ author.nid }}">{{ author.name }}</option>
37                             {% endif %}
38                         {% endfor %}
39                     </select>
40                 </p>
41                 <p><input type="submit" value="修改" class="form-control btn btn-success"></p>
42 
43 
44             </form>
45 
46         </div>
47     </div>
48 </div>
49 
50 </body>
51 </html>
edit_book

 

posted @ 2018-09-11 15:19  Roc_Atlantis  阅读(287)  评论(0编辑  收藏  举报