Django小项目: 图书管理系统
Django基础已经学习结束了,做个小项目巩固一下
- 创建一个Django项目
- 创建一个app
- 配置settings设置
- 配置models数据库
- app01/models.py
class Author(models.Model): # 比较常用的信息放到这个表里面 name = models.CharField(max_length=32) age = models.IntegerField() # authorDetail=models.OneToOneField(to="AuthorDetail",to_field="nid",on_delete=models.CASCADE) # 一对一到AuthorDetail表 生成为表字段之后,会自动变为authorDetail_id这样有一个名称 authorDetail = models.OneToOneField(to='AuthorDetail') # 外键字段 -- 外键字段名_id # foreign+unique def __str__(self): return self.name # 作者详细信息表 class AuthorDetail(models.Model): birthday = models.DateField() # telephone=models.BigIntegerField() telephone = models.CharField(max_length=32) addr = models.CharField(max_length=64) def __str__(self): return self.addr # 出版社表 和 书籍表 是 一对多的关系 class Publish(models.Model): name = models.CharField(max_length=32) city = models.CharField(max_length=32) email = models.EmailField() # charfield -- asdfasdf def __str__(self): return self.name # 书籍表 class Book(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(max_length=32) publishDate = models.DateField() price = models.DecimalField( max_digits=5, decimal_places=2) # decimal(16,2) publishs = models.ForeignKey(to="Publish") authors = models.ManyToManyField(to='Author',) def __str__(self): return self.title
- 同步数据库之后 就可以写视图和路由函数了
from django.conf.urls import url from django.contrib import admin from libry import views urlpatterns = [ url(r'^admin/', admin.site.urls), # 查看所有书籍 url(r'^$', views.showbooks, name='showbooks'), # 添加书籍 url(r'^addbooks/', views.addbooks, name='addbooks'), # 编辑书籍 url(r'^editbooks/(\d+)/', views.editbooks, name='editbooks'), # 删除书籍 url(r'^deletebooks/(\d+)/', views.deletebooks, name='deletebooks'), ]
- app01/views.py
from django.shortcuts import redirect, render from libry import models # Create your views here. # 查看所有书籍 def showbooks(request): all_books = models.Book.objects.all() return render(request, 'showbooks.html', {'all_books': all_books}) # 新增书籍 def addbooks(request): if request.method == 'GET': publish_objs = models.Publish.objects.all() authors_objs = models.Author.objects.all() return render(request, 'add_book.html', {'publish_objs': publish_objs, 'authors_objs': authors_objs}) else: # print(request.POST) authors = request.POST.getlist('authors') # print(authors) all_data = request.POST.dict() # print(all_data) del all_data['csrfmiddlewaretoken'] del all_data['authors'] print(all_data) # title = request.POST.get('book_title') # price = request.POST.get('book_price') # pubdate = request.POST.get('book_pub_date') # publishs = request.POST.get('publishs') # # authors = request.POST.get('authors') # authors = request.POST.getlist('authors') print(all_data) new_book_obj = models.Book.objects.create( **all_data ) new_book_obj.authors.add(*authors) # print(title) # print(price) # print(pubdate) # print(publishs) # print(authors) return redirect('showbooks') # 修改书籍 def editbooks(request, book_id): book_obj = models.Book.objects.filter(pk=book_id) if request.method == 'GET': book_obj = book_obj.first() publish_objs = models.Publish.objects.all() authors_objs = models.Author.objects.all() return render(request, 'edit_book.html', {'book_obj': book_obj, 'publish_objs': publish_objs, 'authors_objs': authors_objs}) else: authors = request.POST.getlist('authors') all_data = request.POST.dict() del all_data['csrfmiddlewaretoken'] del all_data['authors'] print(all_data) book_obj.update(**all_data) book_obj.first().authors.set(authors) return redirect('showbooks') # 删除书籍 def deletebooks(request, book_id): models.Book.objects.filter(pk=book_id).delete() return redirect('showbooks')
添加前端页面
- showbooks.html 展示书籍页面
<body> <h1>查看书籍</h1> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div> <a href="{% url 'addbooks' %}" class="btn btn-primary">添加书籍</a> </div> <table class="table table-hover table-striped"> <thead> <tr> <th>编号</th> <th>书籍名称</th> <th>价格</th> <th>出版日期</th> <th>出版社</th> <th>作者</th> <th>操作</th> </tr> </thead> <tbody> {% for book in all_books %} <tr> <td>{{ forloop.counter }}</td> <td>{{ book.title }}</td> <td>{{ book.price }}</td> <td>{{ book.publishDate|date:'Y-m-d' }}</td> <td>{{ book.publishs }}</td> <td>{{ book.get_author_name }}</td> <td> <a href="{% url 'editbooks' book.pk %}" class="btn btn-warning btn-sm">编辑</a> <a href="{% url 'deletebooks' book.nid %}" class="btn btn-danger btn-sm">删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body>
- add_book.html 添加书籍页面
<body> <h1>添加书籍</h1> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <form action="" method="post" novalidate> {% csrf_token %} <div class="form-group"> <label for="title">书籍名称</label> <input type="text" class="form-control" id="title" placeholder="书名" name="title"> </div> <div class="form-group"> <label for="price">价格</label> <input type="number" class="form-control" id="price" placeholder="价格" name="price"> </div> <div class="form-group"> <label for="date">出版日期</label> <input type="date" class="form-control" id="date" placeholder="日期" name="publishDate"> </div> <div class="form-group"> <label for="publishs">出版社</label> <select name="publishs_id" id="publishs" class="form-control"> {% for publish_obj in publish_objs %} <option value="{{ publish_obj.id }}">{{ publish_obj.name }}</option> {% endfor %} </select> </div> <div class="form-group"> <label for="authors">作者</label> <select name="authors" id="authors" class="form-control" multiple> {% for author_obj in authors_objs %} <option value="{{ author_obj.id }}">{{ author_obj.name }}</option> {% endfor %} </select> </div> <button class="btn btn-success pull-right">提交</button> </form> </div> </div> </div> </body>
- edit_book.html 修改书籍页面
<body> <h1>编辑书籍</h1> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <form action="" method="post" novalidate> {% csrf_token %} <div class="form-group"> <label for="title">书籍名称</label> <input type="text" class="form-control" id="title" placeholder="书名" name="title" value="{{ book_obj.title }}"> </div> <div class="form-group"> <label for="price">价格</label> <input type="number" class="form-control" id="price" placeholder="价格" name="price" value="{{ book_obj.price }}"> </div> <div class="form-group"> <label for="date">出版日期</label> <input type="date" class="form-control" id="date" placeholder="日期" name="publishDate" value="{{ book_obj.publishDate|date:'Y-m-d' }}"> </div> <div class="form-group"> <label for="publishs">出版社</label> <select name="publishs_id" id="publishs" class="form-control"> {% for publish_obj in publish_objs %} {% if publish_obj.pk == book_obj.publishs.pk %} <option value="{{ publish_obj.id }}" selected>{{ publish_obj.name }}</option> {% else %} <option value="{{ publish_obj.id }}">{{ publish_obj.name }}</option> {% endif %} {% endfor %} </select> </div> <div class="form-group"> <label for="authors">作者</label> <select name="authors" id="authors" class="form-control" multiple> {% for author_obj in authors_objs %} {% if author_obj in book_obj.authors.all %} <option value="{{ author_obj.id }}" selected>{{ author_obj.name }}</option> {% else %} <option value="{{ author_obj.id }}">{{ author_obj.name }}</option> {% endif %} {% endfor %} </select> </div> <button class="btn btn-success pull-right">提交</button> </form> </div> </div> </div> </body>

浙公网安备 33010602011771号