urls:
from django.contrib import adminfrom django.urls import path,re_pathfrom first import viewsurlpatterns = [ path('admin/', admin.site.urls), path('index/',views.index, name='index'), #显示的主界面 # path('one/',views.one) path('add/',views.add,name='add'), #添加界面 # re_path('del_me/(?P\d+)',views.del_message,name = 'edit_book'), re_path('del_book/',views.del_message,name='d_book'), #删除 re_path('edit_book/(\d+)/',views.edit_book,name ='edit_book'), #编辑 re_path('login/',views.login,name='login'), #登陆 path('logout/',views.logout, name='logout'), #注销]
views:
from django.shortcuts import render,redirect,HttpResponse,reversefrom first.models import Book,Publish,Author,Gfriend,Userimport jsonimport datetime# Create your views here.# 等一一个装饰器来验证是否可以登陆后进行操作def required_login(func): def inner(*args, **kwargs): request = args[0] # 因为你的参数实在args内 所以你的接收的内容也是args if request.COOKIES.get('is_login'): return func(*args, **kwargs) else: return redirect(reverse('login')) return inner@required_logindef index(request): book_obj = Book.objects.all() author_obj = Author.objects.all() publish_obj = Publish.objects.all() # book_author = Book.authors.all() # print(book_obj) # print(1111) return render(request, 'index.html',locals())@required_logindef add(request): author_obj = Author.objects.all() publish_obj = Publish.objects.all() if request.method =="POST": name = request.POST.get('name') price = request.POST.get('price') dat = request.POST.get('datetime') publish = request.POST.get('publish') author_list =request.POST.getlist('authors') #获取的作者是多个就需要用getlist来获取 print(author_list) book = Book.objects.create(name = name,price = price,date = dat,publish_id = publish ) # book.publish_id.add(publish) book.authors.set(author_list) #用set依次添加 return redirect('/index/') # ut = reverse('add') return render(request,'add.html',locals())@required_logindef del_message(request): del_id = request.POST.get('del_id') # Book.objects.filter(id = del_id).delete() del_book = Book.objects.filter(id = del_id) # print(del_book) del_book.delete() # return redirect('/index/' #删除成功过后你需要进行发送一个信息 不能重定向了 因为用的是ajax局部刷新的 return HttpResponse(json.dumps({ 'status':1})) #返送一个序列化字符串@required_logindef edit_book(request,edid): book_obj = Book.objects.get(id = edid) author_obj = Author.objects.all() publish_obj = Publish.objects.all() if request.method == 'POST': name = request.POST.get('name') price = request.POST.get('price') #get这个值是获取你前端界面的name的属性 value的属性是显示的属性 date = request.POST.get('datetime') publish = request.POST.get('publish') authors = request.POST.getlist('authors') book_obj.name = name book_obj.price = price book_obj.date = date book_obj.publish_id = publish book_obj.authors.set(authors) book_obj.save() return redirect('/index/') return render(request,'edit_book.html',locals())# @required_logindef login(request): if request.method == 'POST': name = request.POST.get('user') pwd = request.POST.get('pwd') user_obj = User.objects.filter(name = name,pwd=pwd) print(user_obj) user_obj = user_obj.first() #因为你取到的user_obj是一个queryset所以你要进行取到第一个是对象 if user_obj: #登陆成功 # 然后用cookie ret = redirect(reverse('index')) #登陆成功后跳转到index界面 这个用的反向解析 ret.set_cookie('name',name) ret.set_cookie('pwd',pwd) ret.set_cookie('is_login',True) #然后你求登陆时间 选哟调用你你数据库中的上一次登陆成功后存储的时间 ret.set_cookie('last_time',user_obj.date) user_obj.date = datetime.datetime.now() #把你这一次登陆的时间再设置回数据库 user_obj.save() return ret return render(request,'login.html')@required_logindef logout(request): ret = redirect(reverse('login')) #退还到你的登陆框 ret.delete_cookie('is_login') ret.delete_cookie('user') ret.delete_cookie('last_time') return ret
models:(数据库):
from django.db import models# Create your models here.class Book(models.Model): name = models.CharField(max_length = 30) price = models.DecimalField(max_digits=30,decimal_places=3) date = models.DateTimeField() publish = models.ForeignKey(to="Publish",to_field='id',on_delete = models.CASCADE) authors = models.ManyToManyField(to='Author')class Publish(models.Model): title = models.CharField(max_length = 30) addr = models.CharField(max_length = 30)class Author(models.Model): name = models.CharField(max_length = 30) addr = models.CharField(max_length = 30) gf = models.OneToOneField(to='Gfriend',to_field='id',on_delete=models.CASCADE)class Gfriend(models.Model): name = models.CharField(max_length =30) age = models.IntegerField()class User(models.Model): name = models.CharField(max_length = 30) pwd = models.IntegerField() date = models.DateTimeField()
template:(前端界面)
index(显示的主界面):
Title 添加 { % for foo in book_obj %} { # { { forloop.count 是自动为它排序的 }}#} { % endfor %}
序号 | 名字 | 价格 | 出版日期 | 出版社 | 作者 | 操作 | 操作 |
---|---|---|---|---|---|---|---|
{ { forloop.counter }} | { { foo.name }} | { { foo.price }} | { { foo.date|date:'Y-m-d' }} | { { foo.publish.title }} | { % for i in foo.authors.all %} { # {% for i in book_author %}#} { % if not forloop.last %} { { i.name }}{ { '|' }} { % else %} { { i.name }} { % endif %} { % endfor %} | 编辑 | 删除 |
add(添加书籍):
Title
编辑:(edit_book)
Title
登陆界面:
Title { # #}
记得不要忘记配置static你所引入的前端样式 还有settings内的数据库配置:
settings:
配置数据库:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'test', # 要连接的数据库,连接前需要创建好 'USER':'root', # 连接数据库的用户名 'PASSWORD':'zhaoyun', # 连接数据库的密码 'HOST':'127.0.0.1', # 连接主机,默认本级 'PORT':3306 # 端口 默认3306 }}配置static:STATICFILES = [ os.path.join(BASE_DIR,'static')]