from django.db import models
# Create your models here.
from django.contrib.auth.models import AbstractUser
class Userinfo(AbstractUser):
phone = models.BigIntegerField(null=True)
desc = models.TextField(null=True)
class Book(models.Model):
"""图书表"""
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=32, verbose_name='书名')
price = models.DecimalField(max_digits=9, decimal_places=3, verbose_name='价格')
publish_time = models.DateTimeField(auto_now_add=True)
"""一对多字段"""
publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
"""多对多字段"""
authors = models.ManyToManyField(to='Author')
def __str__(self):
return f'图书对象<<*>>{self.title}'
class Publish(models.Model):
"""出版社表"""
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32, verbose_name='出版社名称')
address = models.CharField(max_length=64, verbose_name='地址')
def __str__(self):
return f'出版社对象<<*>>{self.name}'
class Author(models.Model):
"""作者表"""
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32, verbose_name='作者姓名')
age = models.IntegerField(verbose_name='年龄')
"""一对一字段"""
author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)
def __str__(self):
return f'作者对象<<*>>{self.name}'
class AuthorDetail(models.Model):
id = models.AutoField(primary_key=True)
phone = models.BigIntegerField(verbose_name='电话号码')
address = models.CharField(max_length=64, verbose_name='作者地址')
def __str__(self):
return f'作者详情表对象<<*>>{self.address}'
.
.
总路由
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# 路由分发
path('app01/', include('app01.urls'))
]
------------------------------------------------
分路由
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('home/', views.home_func, name='home_view'),
# 图书列表展示页
path('book_list/', views.book_list_func, name='book_list_view'),
# auth用户注册
path('register/', views.register_func, name='register_func_view'),
# auth用户登录
path('login/', views.login_func, name='login_func_view'),
# auth修改密码
path('set_pwd/', views.set_pwd_func, name='set_pwd_func_view'),
# auth用户退出登录
path('logout/', views.logout_func, name='logout_func_view'),
# 图书数据添加页
path('book_add/', views.book_add_func, name='book_add_view'),
# 图书数据编辑页
path('book_edit/<int:book_pk>', views.book_edit_func, name='book_edit_view')
]
.
.
from django.shortcuts import render, HttpResponse, redirect, reverse
# Create your views here.
from app01 import models
def home_func(request):
return render(request, 'homePage.html')
from django.contrib import auth
from app01 import models
def register_func(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
confirm_password = request.POST.get('confirm_password')
if not password == confirm_password:
return HttpResponse('两次密码不一致')
# 校验用户名是否存在
res = models.Userinfo.objects.filter(username=username)
if res:
return HttpResponse('用户名已存在')
# 注册用户(create方法不能用)
models.Userinfo.objects.create_user(username=username, password=password)
return render(request, 'registerPage.html')
def login_func(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
# 校验用户名与密码是否正确
user_obj = auth.authenticate(request, username=username, password=password)
# 如果用户名与密码正确,自动生成session数据
if user_obj:
auth.login(request, user_obj)
target_path = request.GET.get('next')
if target_path:
obj = redirect(target_path)
else:
obj = redirect(reverse('home_view'))
request.session.set_expiry(2419200)
return obj
else:
return HttpResponse('密码不正确')
return render(request, 'loginPage.html')
# 写装饰器
from django.contrib.auth.decorators import login_required
@login_required(login_url='/app01/login/')
def book_list_func(request):
book_queryset = models.Book.objects.all()
return render(request, 'bookListPage.html', locals())
@login_required(login_url='/app01/login/')
def set_pwd_func(request):
if request.method == 'POST':
old_pwd = request.POST.get('old_pwd')
new_pwd = request.POST.get('new_pwd')
confirm_pwd = request.POST.get('confirm_password')
print(new_pwd)
print(confirm_pwd)
if not new_pwd == confirm_pwd:
return HttpResponse('两次密码不一致')
# 判断原密码是否正确
is_right = request.user.check_password(old_pwd)
if not is_right:
return HttpResponse('原密码不正确')
# 修改密码
request.user.set_password(new_pwd)
request.user.save()
return HttpResponse('密码修改成功')
return render(request, 'setPwdPage.html')
@login_required(login_url='/app01/login/')
def logout_func(request):
auth.logout(request)
return HttpResponse('退出登录')
@login_required(login_url='/app01/login/')
def book_add_func(request):
# 2. 获取页面上用户输入的值
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
publish_time = request.POST.get('publish_time')
publish_id = request.POST.get('publish_id')
author_id_list = request.POST.getlist('author_id_list')
# 3. 对获取的数据进行写判断
if len(title) == 0 or len(price) == 0:
return HttpResponse('书名与价格不能为空')
# 4. 判断书名是否已存在
res = models.Book.objects.filter(title=title)
if res:
return HttpResponse('书名已存在!')
# 5. 没有问题后往数据库里面书籍表里添加数据
book_obj = models.Book.objects.create(title=title,
price=price,
publish_time=publish_time,
publish_id=publish_id, )
book_obj.authors.add(*author_id_list)
# 6. 重定向到书籍展示页
return redirect(reverse('book_list_view'))
# 1. 分析可得html页面需要全部的出版社数据与全部的作者数据!!
publish_queryset = models.Publish.objects.all()
author_queryset = models.Author.objects.all()
return render(request, 'bookAddPage.html', locals())
---------------------------------------------------
def book_edit_func(request, book_pk):
# 1. 根据传到视图函数的数据主键值查询数据对象
edit_book_obj = models.Book.objects.filter(pk=book_pk).first() # first()不要忘了
# 4. 获取用户编辑后发送的post请求后的数据
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
publish_time = request.POST.get('publish_time')
publish_id = request.POST.get('publish_id')
author_id_list = request.POST.getlist('author_id_list')
# 对获取的数据进行写判断
if len(title) == 0 or len(price) == 0:
return HttpResponse('书名与价格不能为空')
# 5. 没有问题后往数据库里面书籍表里添加数据
models.Book.objects.filter(pk=book_pk).update(title=title,
price=price,
publish_time=publish_time,
publish_id=publish_id)
edit_book_obj.authors.set(author_id_list)
return redirect(reverse('book_list_view'))
# 3. 分析可得编辑页面也需要所有的出版社和作者信息!!!直接复制上面视图函数的代码!!
publish_queryset = models.Publish.objects.all()
author_queryset = models.Author.objects.all()
# 2. 传递给html页面,并展示给用户看
return render(request, 'bookEditPage.html', locals())
.
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static1/jQuery3.6.js"></script>
{% load static %}
<link rel="stylesheet" href={% static "bootstrap-3.4.1-dist/css/bootstrap.min.css" %}>
<script src={% static 'bootstrap-3.4.1-dist/js/bootstrap.min.js' %}></script>
{% block css %}
{% endblock %}
</head>
<body>
<!--导航条-->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">BMS</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">图书<span class="sr-only">(current)</span></a></li>
<li><a href="#">作者</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">快点行动</a></li>
<li><a href="#">等不了了</a></li>
<li><a href="#">快点我</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">更多美女</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">快速赚钱法</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="书名">
</div>
<button type="submit" class="btn btn-default">查询</button>
</form>
<ul class="nav navbar-nav navbar-right">
{% if request.user.is_authenticated %}
<li><a href="#">{{ request.user.username }}</a></li>
{% else %}
<li><a href="{% url 'register_func_view' %}">注册</a></li>
<li><a href="{% url 'login_func_view' %}">登录</a></li>
{% endif %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多操作<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="{% url 'logout_func_view' %}">账号退出</a></li>
<li><a href="{% url 'set_pwd_func_view' %}">修改密码</a></li>
<li><a href="#">投诉建议</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">联系管理员</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<!--左边栏-->
<div class="container-fluid">
<div class="row">
<div class="col-md-2">
<div class="list-group" >
<a href="{% url 'home_view' %}" class="list-group-item active text-center" id="exit1">首页展示</a>
<a href="{% url 'book_list_view' %}" class="list-group-item text-center" id="exit2">图书列表</a>
<a href="#" class="list-group-item text-center" id="exit3">出版社列表</a>
<a href="#" class="list-group-item text-center" id="exit4">作者列表</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
<a href="#" class="list-group-item text-center">跟多详情</a>
</div>
</div>
<!--右边栏-->
<div class="col-md-10">
<!--面板-->
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-th-list" id="span1"></span>图书管理系统</h3>
</div>
<div class="panel-body">
{% block content %}
<!--页头-->
<div class="page-header">
<h1>图书管理系统 <small id="small1">更多精彩等你来查</small></h1>
</div>
<!--巨幕-->
<div class="jumbotron" id="d1">
<h1>全球最大的网上图书管理系统</h1>
<p id="p1">友情连接</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">点击精彩</a>
<button type="button" class="btn btn-primary btn-lg" aria-label="Left Align">
<span class="glyphicon glyphicon-lock " aria-hidden="true"></span>
</button>
</p>
</div>
<!--"右下图片区"-->
<div class="row">
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn>
<img src="xxx" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn>
<img src="xxx" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn>
<img src="xxx" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn> <sapn class="glyphicon glyphicon-heart"></sapn>
<img src="xxx" alt="...">
</a>
</div>
</div> <!--"内部的row"-->
{% endblock %}
</div> <!--"panel-body"-->
</div> <!--"panel panel-primary"-->
</div> <!--"col-md-10"-->
</div> <!--"row"-->
</div> <!--"container-fluid"-->
{% block js %}
{% endblock %}
</body>
</html>
.
.
{% extends 'homePage.html' %}
{% block css %}
{% endblock %}
<!--内容区-->
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form action="" method="post">
{% csrf_token %}
<p>用户名:
<input type="text" name="username" class="form-control">
</p>
<p>密码:
<input type="text" name="password" class="form-control">
</p>
<p>确认密码:
<input type="text" name="confirm_password" class="form-control">
</p>
<input type="submit" value="注册" class="btn btn-primary btn-block">
</form>
</div>
</div>
{% endblock %}
{% block js %}
{% endblock %}
.
.
{% extends 'homePage.html' %}
{% block css %}
{% endblock %}
<!--内容区-->
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form action="" method="post">
{% csrf_token %}
<p>用户名:
<input type="text" name="username" class="form-control">
</p>
<p>密码:
<input type="text" name="password" class="form-control">
</p>
<input type="submit" value="登录" class="btn btn-primary btn-block">
</form>
</div>
</div>
{% endblock %}
。
。
改密码
{% extends 'homePage.html'%}
{% block css %}
{% endblock %}
<!--内容区-->
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form action="" method="post">
{% csrf_token %}
<p>原密码:
<input type="text" name="old_pwd" class="form-control">
</p>
<p>新密码:
<input type="text" name="new_pwd" class="form-control">
</p>
<p>确认密码:
<input type="text" name="confirm_password" class="form-control">
</p>
<input type="submit" value="确认修改密码" class="btn btn-primary btn-block">
</form>
</div>
</div>
{% endblock %}
{% block js %}
{% endblock %}
.
.
booklistpage
{% extends 'homePage.html' %}
{% block css %}
{% endblock %}
<!--内容区-->
{% block content %}
<h2 class="text-center">图书展示页</h2>
<a href="{% url 'book_add_view' %}" class="btn btn-warning">图书添加</a>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>价格</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book_obj in book_queryset %} <!--QuerySet[书对象,书对象]-->
<tr>
<td>{{ forloop.counter }}</td> <!--可以展示主键值也可以展示从一开始的有序数列-->
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.price }}</td>
<td>{{ book_obj.publish_time|date:'Y-m-d' }}</td>
<td class="btn btn-info">{{ book_obj.publish.name }}</td>
<td ><!--注意多对多的这张表是一个虚拟表,通过书对象点虚拟外键字段authors只会跳到Author作者表,不会往虚拟表上添加!!-->
{% for author_obj in book_obj.authors.all %} <!--QuerySet[作者对象,作者对象]-->
<span style="margin-right: 5px" class="btn btn-success">{{ author_obj.name }}</span>
{# {% if forloop.last %}#}
{# {{ author_obj.name }}#}
{# {% else %}#}
{# {{ author_obj.name }} /#}
{# {% endif %} <!--for循环出的是最后一个不加分隔符,其他的末尾加个分隔符!!-->#}
{% endfor %}
</td>
<td>
<!--先拿到别名'book_edit_view'反向解析出路由book_edit/ 再加上for循环出的每一个书籍对象的主键值,一起拼出新的路由-->
<!--a标签的作用是跳转到这个新的路由网址上去,跳转到新的路由网址上去后,首先还是要先到路由层匹配视图函数-->
<!--当匹配上后,将request与book_pk=从网址上匹配到的动态的路由结果,再传递给视图函数!!-->
<a href="{% url 'book_edit_view' book_obj.pk %} " class="btn btn-primary btn-xs">编辑</a>
<a href=" " class="btn btn-danger btn-xs delBtn">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<!--分页!!-->
<nav aria-label="Page navigation" class="text-center">
<ul class="pagination"> <!--这个是属性是让列表标签浮动起来,让原来竖排变成横排!!-->
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#">8</a></li>
<li><a href="#">9</a></li>
<li><a href="#">10</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
{% endblock %}
<!--js区-->
<!--给删除按钮添加一个删除点击确认事件!!-->
{% block js %}
<script>
$('.delBtn').click(function () {
let res = confirm('你确定要删除吗?')
if (!res){
return false
}
})
</script>
{% endblock %}
。
。
bookaddpage
{% extends 'homePage.html' %}
{% block css %}
{% endblock %}
<!--内容区-->
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2 class="text-center">图书添加页</h2>
<form action="" method="post">
{% csrf_token %}
<p> 书名:
<input type="text" name="title" class="form-control">
</p>
<p> 价格:
<input type="text" name="price" class="form-control">
</p>
<p> 出版日期:
<input type="date" name="publish_time" class="form-control">
</p>
<p>出版社名称:
<!--展示系统中已有的出版社信息供选择-->
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_queryset %}
<option value="{{ publish_obj.id }}">{{ publish_obj.name }}</option>
{% endfor %}
</select>
</p>
<p>作者名称list:
<select name="author_id_list" id="" multiple class="form-control">
{% for author_obj in author_queryset %}
<option value="{{ author_obj.id }}">{{ author_obj.name }}</option>
{% endfor %}
</select>
</p>
<input type="submit" value="书籍的添加" class="btn btn-block btn-success">
</form>
</div>
</div>
{% endblock %}
{% block js %}
{% endblock %}
.
.
bookeditpage
<!--书籍编辑页-->
{% extends 'homePage.html' %} <!--继承母板-->
<!--css区-->
{% block css %}
{% endblock %}
<!--内容区-->
{% block content %}
<div class="row">
<div class="col-md-6">
<h2 class="text-center">图书编辑页</h2>
<form action="" method="post" >
{% csrf_token %}
<p> 书名:
<input type="text" name="title" class="form-control" value="{{ edit_book_obj.title }}">
</p>
<p> 价格:
<input type="text" name="price" class="form-control" value="{{ edit_book_obj.price }}">
</p>
<p> 出版日期:
<input type="date" name="publish_time" class="form-control" value="{{ edit_book_obj.publish_time|date:'Y-m-d' }}">
</p>
<p> 出版社名称:
<!--展示系统中已有的出版社信息供选中-->
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_queryset %} <!--queryset [数据对象,数据对象]-->
<!--根据待编辑的书籍对象查询对应的出版社对象,与系统所有的出版社对象比对,如果相同则添加默认选中的属性selected-->
{% if edit_book_obj.publish == publish_obj %}
<option value="{{ publish_obj.id }}"selected>{{ publish_obj.name }}</option>
{% else %}
<option value="{{ publish_obj.id }}">{{ publish_obj.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<p> 作者名称list:
<select name="author_id_list" id="" multiple class="form-control"> <!--一个图书可以有多个作者,加个multiple可以多选-->
{% for author_obj in author_queryset %} <!--queryset [数据对象,数据对象]-->
<!--成员运算判断判断,循环的作者对象是否在待编辑的书籍作者对象列表中-->
{% if author_obj in edit_book_obj.authors.all %}
<option value="{{ author_obj.id }}"selected>{{ author_obj.name }}</option>
{% else %}
<option value="{{ author_obj.id }}">{{ author_obj.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<input type="submit" value="书籍的编辑" class="btn btn-block btn-danger">
</form>
</div> <!--"col-md-6"-->
<div class="col-md-6">
<img style="max-width:100%" src="https://file.moyublog.com/d/file/2021-02-14/c7e482715ac8cd045c3964b147231ee8.jpg" alt="..." class="img-rounded">
</div>
</div> <!--"row"-->
{% endblock %}
<!--js区-->
{% block js %}
{% endblock %}
标签:obj,name,框架,models,request,publish,book,-----,第十三天
From: https://www.cnblogs.com/tengyifan888/p/17009316.html