一、系统功能
1、用户通过用户名密码登陆图书管理页面
2、在管理页面可进行添加、编辑、删除书籍
3、前端进行增删改时,数据库需同步进行操作
4、可随时注销用户,退出登陆并清除cookie缓存
二、页面效果
1、登陆界面
用户通过指定用户名密码登陆后,发起session会话,浏览器存储session_id
2、管理页面
在管理页面用户可进行增删改
3、添加和编辑页面
添加和编辑页面大体一致
三、Django代码
1、目录结构
2、关键文件代码
(1)后端
views.py:
from django.shortcuts import render,HttpResponse,redirect
from app01 import models
# Create your views here.
def Book(request):
ret=models.Book.objects.all()
return render(request,'primary_page.html',{
'ret':ret})
def delete(request,n):
models.Book.objects.get(id=n).delete()
return redirect('/book/')
def editpg(request,n):
ret=models.Book.objects.filter(id=n)
return render(request,'editpage.html',{
'ret':ret})
def edit(request,n):
if request.method == 'POST':
title=request.POST.get('title')
price=request.POST.get('price')
date=request.POST.get('date')
publish=request.POST.get('publish')
author = request.POST.getlist('author')
author_table = {
'Lsir':1,'Jsir':2,'Zsir':3,'Ysir':4,'Wsir':5}
author_id=[]
for i in author:
value=str(author_table[i])
author_id.append(value)
publish_id=None
if publish == "人民出版社":
publish_id = 1
elif publish == "清华出版社":
publish_id = 2
elif publish == "天一出版社":
publish_id = 3
models.Book.objects.filter(id=n).update(
title=title,
price=price,
publishDate=date,
publish_id=publish_id,
)
ret=models.Book.objects.get(id=n)
ret.authors.set(author_id)
home=redirect('/book/')
return home
def addpg(request):
if request.method == 'POST':
title=request.POST.get('title')
price=request.POST.get('price')
date=request.POST.get('date')
publish=request.POST.get('publish')
publish_id = None
if publish == "人民出版社":
publish_id = 1
elif publish == "清华出版社":
publish_id = 2
elif publish == "天一出版社":
publish_id = 3
author=request.POST.getlist('author')
author_table={
'Lsir':1,'Jsir':2,'Zsir':3,'Ysir':4,'Wsir':5}
author_id=[]
for i in author:
value=author_table[i]
author_id.append(value)
models.Book.objects.create(
title=title,
price=price,
publishDate=date,
publish_id=publish_id
)
book_id=models.Book.objects.filter(title=title).values('id')
obj=models.Book.objects.get(id=book_id)
obj.authors.add(*author_id)
home=redirect('/book/')
return home
else:
page=render(request,'addpage.html')
return page
def login(request):
if request.method == 'GET':
home=render(request,'login.html')
return home
else:
if request.POST.get('username') == 'root' and request.POST.get('password') == 'password':
request.session['is_login']=True
sign=HttpResponse('ok')
return sign
else:
sign = HttpResponse('error')
return sign
def loginout(request):
request.session.flush()
res = HttpResponse('ok')
return res
models.py文件:
from django.db import models
# Create your models here.
class Author(models.Model):
name=models.CharField(max_length=32)
age=models.IntegerField()
ad=models.OneToOneField(to='AuthorDetail',to_field='id',on_delete=models.CASCADE)
class AuthorDetail(models.Model):
birthday=models.DateField(auto_now=True)
telephone=models.CharField(max_length=32)
addr=models.CharField(max_length=32)
class Publishs(models.Model):
name=models.CharField(max_length=32)
city=models.CharField(max_length=32)
class Book(models.Model):
title=models.CharField(max_length=32)
publishDate=models.DateField(auto_now=True)
price=models.DecimalField(max_digits=5,decimal_places=2)
publish=models.ForeignKey(to='Publishs')
authors=models.ManyToManyField(to='Author')
urls.py路由配置:
from django.conf.urls import url
from django.cont