實戰圖書館html
from django.shortcuts import render,redirect,HttpResponse from app01 import models # Create your views here. def author_list(request): all_author=models.Author.objects.all() return render(request, 'author_list.html', {'all_author': all_author}) def author_add(request): if request.method=='GET': all_book = models.Book.objects.all() return render(request,'author_add.html',{'all_book':all_book}) else: name=request.POST.get('name') aid=request.POST.get('aid') book_obj = models.Book.objects.get(id=aid) author_obj=models.Author.objects.create(name=name) book_obj.authors.add(author_obj) #正向添加(由於多對多的外鍵在book表裏)就是:book對象.外鍵.add(另一張表的對象) return redirect('/author_list/') def author_edit(request): if request.method=='GET': pk=request.GET.get('id') all_author=models.Author.objects.filter(pk=pk).first() all_book = models.Book.objects.all() return render(request,'author_edit.html',{'all_author':all_author,'all_book':all_book}) else: name = request.POST.get('name') aid = request.POST.get('aid') pk = request.GET.get('id') book_obj = models.Book.objects.get(id=aid) author_obj = models.Author.objects.filter(pk=pk) author_obj.update(name=name) book_obj.authors.set([pk,]) # 正向添加(由於多對多的外鍵在book表裏)就是:book對象.外鍵.add(另一張表的對象) return redirect('/author_list/') def author_del(request): if request.method == 'GET': pk = request.GET.get('id') models.Author.objects.filter(pk=pk).delete() return redirect('/author_list/') def AuthorDetail_list(request): if request.method=='GET': all_AuthorDetail = models.AuthorDetail.objects.all() return render(request, 'AuthorDetail_list.html', {'all_AuthorDetail': all_AuthorDetail}) def AuthorDetail_add(request): if request.method=='GET': all_AuthorDetail = models.AuthorDetail.objects.all() return render(request, 'AuthorDetail_add.html', {'all_AuthorDetail': all_AuthorDetail}) else: name=request.POST.get('name') addr=request.POST.get('addr') num=request.POST.get('num') birthday=request.POST.get('birthday') models.Author.objects.create(name=name) #爲啥加不進去 au_obj=models.AuthorDetail.objects.create( addr=addr, num=num, birthday=birthday, authors=models.Author.objects.filter(name=name) ) au_obj.save() return redirect('/AuthorDetail_list/') def AuthorDetail_del(request): pk=request.GET.get('pk') AuthorDetail_all=models.AuthorDetail.objects.filter(pk=pk) AuthorDetail_all.delete() return redirect('/AuthorDetail_list/') def AuthorDetail_edil(request): pk = request.GET.get('pk') AuthorDetail_all = models.AuthorDetail.objects.filter(pk=pk) return render(request, 'AuthorDetail_edil.html', {'AuthorDetail_all': AuthorDetail_all}) def Publisher_list(request): Publisher_all=models.Publisher.objects.all() return render(request,'Publisher_list.html',{'Publisher_all':Publisher_all}) def Publisher_add(request): if request.method=='GET': publish_all=models.Publisher.objects.all() else: name=request.POST.get('name') addr=request.POST.get('addr') models.Publisher.objects.create(name=name,addr=addr) return redirect('/Publisher_list/') return render(request,'Publisher_add.html',{'publish_all':publish_all}) def Publisher_edil(request): if request.method == 'GET': pk=request.GET.get('pk') Publisher_all = models.Publisher.objects.filter(pk=pk) else: name = request.POST.get('name') addr = request.POST.get('addr') pub_obj=models.Publisher.objects.update(name=name, addr=addr) pub_obj.save() #爲何不更新 只是增長 return redirect('/Publisher_list/') return render(request, 'Publisher_edil.html', {'Publisher_all': Publisher_all}) def book_list(request): if request.method == 'GET': book_all = models.Book.objects.all() return render(request, 'book_list.html', {'book_all': book_all}) def book_del(request): models.Book.objects.all().delete() return render(request, 'book_list.html') def book_add(request): if request.method =='GET': publish_all = models.Publisher.objects.all() # print(publish_all.name) return render(request, 'book_add.html', {'publish_all':publish_all}) else: title = request.POST.get('title') price = request.POST.get('price') pubtime = request.POST.get('pubtime') bid = request.POST.get('bid') #得到select標籤所得到的ID book_obj = models.Book.objects.create( title=title, price=price, pubtime=pubtime, publishers_id=bid ) book_obj.save() return redirect('/book_list/') def book_edit(request): pk = request.GET.get('id') book_all = models.Book.objects.filter(pk=pk).first() publish_all = models.Publisher.objects.all() if request.method == 'POST': title = request.POST.get('title') price = request.POST.get('price') pubtime = request.POST.get('pubtime') bid = request.POST.get('bid') # 得到select標籤所得到的ID book_all.title = title, book_all.price = price, book_all.pubtime = pubtime, book_all.publishers_id = bid book_all.save() return redirect('/book_list/') all_pub = models.Publisher.objects.all() return render(request, 'book_edit.html', locals())