django(图书管理系统)
一、表的设计
from django.db import models # Create your models here. class Book(models.Model): title = models.CharField(max_length=32, verbose_name='书名') price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='价格') publish_date = models.DateField(auto_now_add=True, verbose_name='出版时间') publish = models.ForeignKey(to='Publish') author = models.ManyToManyField(to='Author') class Publish(models.Model): name = models.CharField(max_length=32, verbose_name='出版社名') addr = models.CharField(max_length=64, verbose_name='出版社地址') email = models.EmailField(verbose_name='邮箱地址') class Author(models.Model): name = models.CharField(max_length=32, verbose_name='作者名') age = models.IntegerField(verbose_name='年龄') author_detail = models.OneToOneField(to='AuthorDetail') class AuthorDetail(models.Model): phone = models.BigIntegerField(verbose_name='手机号') addr = models.CharField(max_length=64, verbose_name='作者住址')
二、路由层(url.py)
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.home, name='home'), url(r'^book/list/', views.book_list, name='book_list'), # 书籍增加 url(r'^book/add/', views.book_add, name='book_add'), # 书籍的编辑 url(r'^book/edit/(?P<edit_id>\d+)', views.book_edit, name='book_edit'), # 书籍的删除 url(r'^book/delete/(\d+)', views.book_delete, name='book_delete') ]
三、视图层(views.py)
def home(request): return render(request, 'home.html') def book_list(request): book_query = models.Book.objects.all() return render(request, 'book_list.html', locals()) # 书籍增加 def book_add(request): if request.method == 'POST': print(request.POST) title = request.POST.get('title') price = request.POST.get('price') publish_date = request.POST.get('publish_date') publish_id = request.POST.get('publish') author_list = request.POST.getlist('author') # 操作数据库存储数据 # 书籍表 book_obj = models.Book.objects.create(title=title, price=price, publish_date=publish_date, publish_id=publish_id) # 书籍与作者的关系表 book_obj.author.add(*author_list) # *将列表打散 # 跳转到书籍的展示页面 ''' redirect括号内可以直接写url 也可以直接写别名 但是别名需要额外的参数,就必须使用reverse解析 ''' return redirect('book_list') # 获取当前系统所有的出版社和作者信息 publish_queryset = models.Publish.objects.all() author_queryset = models.Author.objects.all() return render(request, 'book_add.html', locals()) def book_edit(request, edit_id): if request.method == 'POST': title = request.POST.get('title') price = request.POST.get('price') publish_date = request.POST.get('publish_date') publish_id = request.POST.get('publish') author_list = request.POST.getlist('author') models.Book.objects.filter(pk=edit_id).update(title=title, price=price, publish_date=publish_date, publish_id=publish_id) # 修改第三张表 book_obj = models.Book.objects.filter(pk=edit_id).first() book_obj.author.set(author_list) return redirect('book_list') # 获取当前用户想要编辑的书籍对象,展示给用户看 edit_obj = models.Book.objects.filter(pk=edit_id).first() publish_queryset = models.Publish.objects.all() author_queryset = models.Author.objects.all() return render(request, 'book_edit.html', locals()) def book_delete(request, delete_id): models.Book.objects.filter(pk=delete_id).delete() return redirect('book_list')
四、首页页面
def home(request):
return render(request, 'home.html')
def book_list(request):
book_query = models.Book.objects.all()
return render(request, 'book_list.html', locals())
# 书籍增加
def book_add(request):
if request.method == 'POST':
print(request.POST)
title = request.POST.get('title')
price = request.POST.get('price')
publish_date = request.POST.get('publish_date')
publish_id = request.POST.get('publish')
author_list = request.POST.getlist('author')
# 操作数据库存储数据
# 书籍表
book_obj = models.Book.objects.create(title=title, price=price, publish_date=publish_date,
publish_id=publish_id)
# 书籍与作者的关系表
book_obj.author.add(*author_list) # *将列表打散
# 跳转到书籍的展示页面
'''
redirect括号内可以直接写url
也可以直接写别名
但是别名需要额外的参数,就必须使用reverse解析
'''
return redirect('book_list')
# 获取当前系统所有的出版社和作者信息
publish_queryset = models.Publish.objects.all()
author_queryset = models.Author.objects.all()
return render(request, 'book_add.html', locals())
def book_edit(request, edit_id):
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
publish_date = request.POST.get('publish_date')
publish_id = request.POST.get('publish')
author_list = request.POST.getlist('author')
models.Book.objects.filter(pk=edit_id).update(title=title, price=price, publish_date=publish_date,
publish_id=publish_id)
# 修改第三张表
book_obj = models.Book.objects.filter(pk=edit_id).first()
book_obj.author.set(author_list)
return redirect('book_list')
# 获取当前用户想要编辑的书籍对象,展示给用户看
edit_obj = models.Book.objects.filter(pk=edit_id).first()
publish_queryset = models.Publish.objects.all()
author_queryset = models.Author.objects.all()
return render(request, 'book_edit.html', locals())
def book_delete(request, delete_id):
models.Book.objects.filter(pk=delete_id).delete()
return redirect('book_list')
五、图书展示页面(book_list.html)
{% extends 'home.html' %} {% block content %} <a href="{% url 'book_add' %}" class="btn-success">添加</a> <br> <table class="table table-hover table-striped"> <thead> <tr> <th>ID</th> <th>书名</th> <th>价格</th> <th>出版日期</th> <th>出版社</th> <th>作者</th> <th>操作</th> </tr> </thead> <tbody> {% for book_obj in book_query %} <tr> <td>{{ book_obj.pk }}</td> <td>{{ book_obj.title }}</td> <td>{{ book_obj.price }}</td> <td>{{ book_obj.publish_date|date:'Y-m-d' }}</td> <td>{{ book_obj.publish.name }}</td> <td> {% for author_obj in book_obj.author.all %} {% if forloop.last %} {{ author_obj.name }} {% else %} {{ author_obj.name }}, {% endif %} {% endfor %} </td> <td> <a href="{% url 'book_edit' book_obj.pk %}" class="btn btn-primary btn-xs">编辑</a> <a href="{% url 'book_delete' book_obj.pk %}" class="btn btn-primary btn-xs">删除</a> </td> </tr> {% endfor %} </tbody> </table> {% endblock %}
六 图书增加(book_add.html)
{% extends 'home.html' %} {% block content %} <h1 class="text-center">书籍添加</h1> <form action="" method="post"> <p>书名: <input type="text" name="title" class="form-control"> </p> <p>价格: <input type="text" name="price" class="form-control"> </p> <p>出版日期: <input type="date" name="publish_date" class="form-control"> </p> <p>出版社: <select name="publish" id="" class="form-control"> {% for publish_obj in publish_queryset %} <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option> {% endfor %} </select> </p> <p>作者: <select name="author" id="" multiple class="form-control"> {% for author_obj in author_queryset %} <option value="{{ author_obj.pk }}">{{ author_obj.name }}</option> {% endfor %} </select> </p> <input type="submit" value="新增" class="btn btn-primary btn-block"> </form> {% endblock %}
七 图书编辑(book_edit.html)
{% extends 'home.html' %} {% block content %} <h1 class="text-center">书籍编辑</h1> <form action="" method="post"> <p>书名: <input type="text" name="title" class="form-control" value="{{ edit_obj.title }}"> </p> <p>价格: <input type="text" name="price" class="form-control" value="{{ edit_obj.price }}"> </p> <p>出版日期: <input type="date" name="publish_date" class="form-control" value="{{ edit_obj.publish_date|date:'Y-m-d' }}"> </p> <p>出版社: <select name="publish" id="" class="form-control"> {% for publish_obj in publish_queryset %} {# 针对当前书籍对象的出版社应该默认选中 #} {% if edit_obj.publish == publish_obj %} <option value="{{ publish_obj.pk }}" selected>{{ publish_obj.name }}</option> {% else %} <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option> {% endif %} {% endfor %} </select> </p> <p>作者: <select name="author" id="" multiple class="form-control"> {% for author_obj in author_queryset %} {% if author_obj in edit_obj.author.all %} <option value="{{ author_obj.pk }}" selected>{{ author_obj.name }}</option> {% else %} <option value="{{ author_obj.pk }}" >{{ author_obj.name }}</option> {% endif %} {% endfor %} </select> </p> <input type="submit" value="确定编辑" class="btn btn-info btn-block"> </form> {% endblock %}