练习-多表图书管理系统
生活随笔
收集整理的這篇文章主要介紹了
练习-多表图书管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
urls
from django.contrib import admin from django.urls import path,re_path from book import viewsurlpatterns = [path('admin/', admin.site.urls),re_path('books/add/$',views.add_book),re_path('books/$',views.books),re_path('books/(\d+)/change/$',views.change_book),re_path('books/(\d+)/delete/$', views.delete_book) ]?
models
from django.db import models# Create your models here. class Author(models.Model):nid = models.AutoField(primary_key=True)name=models.CharField(max_length=32)age=models.IntegerField() class Publish(models.Model):nid=models.AutoField(primary_key=True)name=models.CharField(max_length=32)city=models.CharField(max_length=32)email=models.EmailField()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)publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE)authors=models.ManyToManyField(to="Author")?
views
from django.shortcuts import render,HttpResponse,redirect from .models import Publish, Authorfrom book.models import *# Create your views here.def add_book(request):if request.method == "POST":title=request.POST.get("title")price=request.POST.get("price")pub_date=request.POST.get("pub_date")publish_id=request.POST.get("publish_id")authors_id_list=request.POST.getlist("authors_id_list") #getlist 應用在checkbox,select book_obj=Book.objects.create(title=title,price=price,publishDate=pub_date,publish_id=publish_id)book_obj.authors.add(*authors_id_list)return redirect("/books/")publish_list = Publish.objects.all()author_list = Author.objects.all()return render(request, "addbook.html", {"author_list": author_list,"publish_list": publish_list,})def books(request):book_list=Book.objects.all()return render(request,"books.html",{'book_list':book_list})def change_book(request,edit_book_id):edit_book_obj = Book.objects.filter(pk=edit_book_id).first() #編輯的對象if request.method == "POST":title = request.POST.get("title")price = request.POST.get("price")pub_date = request.POST.get("pub_date")publish_id = request.POST.get("publish_id")authors_id_list = request.POST.getlist("authors_id_list") # getlist 應用在checkbox,select Book.objects.filter(pk=edit_book_id).update(title=title,price=price,publishDate=pub_date,publish_id=publish_id)# edit_book_obj.authors.clear()# edit_book_obj.authors.add(*authors_id_list) edit_book_obj.authors.set(authors_id_list) #set:先清空,再添加值return redirect("/books/")publish_list=Publish.objects.all()author_list=Author.objects.all()return render(request,"editbook.html",{"edit_book_obj":edit_book_obj,"publish_list":publish_list,"author_list":author_list})def delete_book(request,delete_book_id):Book.objects.filter(pk=delete_book_id).delete()return redirect("/books/")?
addbook.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/static/bs/css/bootstrap.css"> </head> <body> <h3>添加書籍</h3> <div class="container"><div class="row"><div class="col-md-6 col-md-offset-3"><form action="" method="post">{% csrf_token %}<div class="form-group"><label for="">名稱</label><input type="text" name="title" class="form-control"></div><div class="form-group"><label for="">價格</label><input type="text" name="price" class="form-control"></div><div class="form-group"><label for="">出版日期</label><input type="date" name="pub_date" class="form-control"></div>單選框:select+for循環+option
option 元素定義下拉列表中的一個選項(一個條目)。
<div class="form-group"><label for="">出版社</label><select name="publish_id" id="" class="form-control" >{% for publish in publish_list %}<option value="{{ publish.pk }}">{{ publish.name }}</option>{% endfor %}</select></div>
復選框:需要注意,select加上multiple(復選框),其余與單選框相同<div class="form-group"><label for="">作者</label><select name="authors_id_list" id="" multiple class="form-control">{% for author in author_list %}<option value="{{ author.pk }}">{{ author.name }}</option>{% endfor %}</select></div><input type="submit" class="btn btn-default"></form></div></div> </div></body> </html>
?
books.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/static/bs/css/bootstrap.css"> </head> <body> <h3>查看書籍</h3> <div class="container"><div class="row"><div class="col-md-8 col-md-offset-2"><a href="/books/add/" class="btn btn-primary">添加書籍</a><table class="table table-bordered 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 book_list %}<tr><td>{{ forloop.counter }}</td><td>{{ book.title }}</td><td>{{ book.price }}</td><td>{{ book.publishDate|date:"Y-m-d" }}</td><td>{{ book.publish.name }}</td><td>{% for author in book.authors.all %}{% if forloop.last %}<span>{{ author.name }}</span>{% else %}<span>{{ author.name }}</span>,{% endif %}{% endfor %}</td><td><a href="/books/{{ book.pk }}/change/" class="btn btn-warning">編輯</a><a href="/books/{{ book.pk }}/delete/" class="btn btn-danger">刪除</a></td></tr>{% endfor %}</tbody></table></div></div> </div></body> </html>?
editbook.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/static/bs/css/bootstrap.css"> </head> <body> <h3>編輯書籍</h3> <div class="container"><div class="row"><div class="col-md-6 col-md-offset-3"><form action="" method="post">{% csrf_token %}<div class="form-group"><label for="">名稱</label><input type="text" name="title" class="form-control" value="{{ edit_book_obj.title }}"></div><div class="form-group"><label for="">價格</label><input type="text" name="price" class="form-control" value="{{ edit_book_obj.price }}"></div><div class="form-group"><label for="">出版日期</label><input type="date" name="pub_date" class="form-control"value="{{ edit_book_obj.publishDate|date:"Y-m-d" }}"></div><div class="form-group"><label for="">出版社</label><select name="publish_id" id="" class="form-control" value="{{ edit_book_obj.publish }}">{% for publish in publish_list %}{% if edit_book_obj.publish == publish %}<option selected value="{{ publish.pk }}">{{ publish.name }}</option>{% else %}<option value="{{ publish.pk }}">{{ publish.name }}</option>{% endif %}{% endfor %}</select></div><div class="form-group"><label for="">作者</label><select name="authors_id_list" id="" multiple class="form-control">{% for author in author_list %}{% if author in edit_book_obj.authors.all %}<option selected value="{{ author.pk }}">{{ author.name }}</option>{% else %}<option value="{{ author.pk }}">{{ author.name }}</option>{% endif %}{% endfor %}</select></div><input type="submit" class="btn btn-default"></form></div></div> </div></body> </html>?
轉載于:https://www.cnblogs.com/hexiaorui123/p/10590783.html
總結
以上是生活随笔為你收集整理的练习-多表图书管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WCF中双工协议
- 下一篇: 良好的JavaScript编码风格(语法