发布时间:2018-07-06 10:46:17编辑:Run阅读(8572)
django创建一个新的项目
设置静态文件,更改settings配置,在最后添加
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
在Bookmanager目录下创建static目录,目录结构如下:
设计表:(表与表的关系,表结构)
修改models.py文件
from django.db import models # Create your models here. class AuthorDetail(models.Model): gf = models.CharField(max_length=32) tel = models.CharField(max_length=32) class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() # 与AuthorDetail建立一对一的关系 # ad=models.ForeignKey(to="AuthorDetail",to_field="id",on_delete=models.CASCADE,unique=True) ad = models.OneToOneField(to="AuthorDetail", to_field="id", on_delete=models.CASCADE, ) class Publish(models.Model): name = models.CharField(max_length=32) email = models.CharField(max_length=32) addr = models.CharField(max_length=32) class Book(models.Model): title = models.CharField(max_length=32, unique=True) price = models.DecimalField(max_digits=8, decimal_places=2, null=True) pub_date = models.DateField() # 与Publish建立一对多的关系,外键字段建立在多的一方 publish = models.ForeignKey(to="Publish", to_field="id", on_delete=models.CASCADE) # 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建关系表book_authors authors = models.ManyToManyField(to="Author") class User(models.Model): name = models.CharField(max_length=32) pwd = models.CharField(max_length=32) last_time = models.DateTimeField()
连接数据库
没有使用pycharm,需执行
python manage.py makemigrations
python manage.py migrate
pycharm点击Tools里面的Run manage.py Task
先执行
manage.py@Bookmanager > makemigrations
再执行
manage.py@Bookmanager > migrate
django默认使用的是sqllite数据库,可以看到5张表已经创建好了
app_author 作者表
app_authordetail 作者详情表
app_book 书籍表
app_book_authors 作者与书籍关系表
app_publish 出版社详情表
app_user 存放账号密码表
往表里面添加数据
app_publish表, 出版社详情表
app_authordetail,作者详情表
app_author,作者表
app_book,书籍表
app_book_authors, 作者书籍关系表
添加路由,修改urls.py文件
from django.contrib import admin from django.urls import path,re_path from app import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.login, name='login'), path('books/', views.books, name='books'), path('add_book/', views.add_book, name='add_book'), re_path('edit_book/(\d+)', views.edit_book, name='edit_book'), re_path('del_book', views.del_book, name='del_book'), re_path('login/', views.login, name='login'), re_path('logout/', views.logout, name='logout'), ]
修改视图函数,views.py文件
from django.shortcuts import render,HttpResponse,redirect,reverse from app import models import json import datetime # Create your views here. # 装饰器 判断session里面是否有is_login参数 def required_login(func): def inner(*args, **kwargs): request = args[0] # cookie写法 # if request.COOKIES.get('is_login'): # session写法 if request.session.get('is_login'): return func(*args, **kwargs) else: if request.is_ajax(): return HttpResponse(json.dumps({'status':0})) return redirect(reverse('login')) return inner @required_login def books(request): books = models.Book.objects.all() return render(request, 'books.html', {'books':books}) @required_login def add_book(request): if request.method == 'POST': print(request.POST) title = request.POST.get('name') price = request.POST.get('price') date = request.POST.get('date') publish = request.POST.get('publish') authors = request.POST.getlist('authors') print(title,price,date,publish,authors) new_book = models.Book.objects.create(title=title,price=price,pub_date=date,publish_id=publish) new_book.authors.add(*authors) return redirect(reverse('books')) publishers = models.Publish.objects.all() authors = models.Author.objects.all() return render(request, 'add_book.html', {'publishers':publishers, 'authors':authors}) @required_login def edit_book(request, edit_id): book_obj = models.Book.objects.get(id=edit_id) if request.method == 'POST': title = request.POST.get('name') price = request.POST.get('price') date = request.POST.get('date') publish = request.POST.get('publish') authors = request.POST.get('authors') book_obj.title = title book_obj.price = price book_obj.pub_date = date book_obj.publish_id = publish book_obj.save() book_obj.authors.set(authors) return redirect(reverse("books")) publishers = models.Publish.objects.all() authors = models.Author.objects.all() return render(request, 'edit_book.html', {'book_obj':book_obj,'publishers':publishers,'authors':authors}) @required_login def del_book(request): # print(request.POST) del_id = request.POST.get('del_id') del_list = models.Book.objects.filter(id=del_id) # print(del_list) del_list.delete() return HttpResponse(json.dumps({'status': 1})) def login(request): if request.method == 'POST': print(request.POST) name = request.POST.get('name') pwd = request.POST.get('pwd') user_list = models.User.objects.filter(name=name, pwd=pwd) if user_list: user_obj = user_list.first() ret = redirect(reverse('books')) # cookie写法 # ret.set_cookie('is_login', True) # ret.set_cookie('user', name) # ret.set_cookie('last_time', user_obj.last_time) # session写法,安全 request.session['is_login'] = True request.session['user'] = name request.session['last_time'] = str(user_obj.last_time) user_obj.last_time = datetime.datetime.now() user_obj.save() return ret return render(request, "login.html") @required_login def logout(request): ret = redirect(reverse('login')) # cookie写法 # ret.delete_cookie('is_login') # ret.delete_cookie('user') # ret.delete_cookie('last_time') # session写法 request.session.flush() return ret
静态文件目录,static
模版templates目录,存放html文件
效果展示:
完整代码放置github
github地址:https://github.com/py3study/book_manage
47604
45983
36909
34467
29079
25713
24565
19714
19245
17756
5564°
6155°
5690°
5737°
6704°
5482°
5484°
5988°
5965°
7295°