创建一个简易的可以增删改查book的书籍管理system

urls.py

from django.contrib import admin
from django.urls import re_path,path
from book import views urlpatterns = [
path('admin/', admin.site.urls),
re_path('books/add/$', views.add_book),
re_path('books/$', views.books),
re_path('layout/$', views.layout),
re_path('motai/$', views.motai),
re_path('books/(\d+)/change/$', views.change_book),
re_path('books/(\d+)/delete/$', views.delete_book),
]

settings.py

ROOT_URLCONF = 'bookms_02.urls'

views.py

from django.shortcuts import render,HttpResponse,redirect

# Create your views here.

from .models import Publish,Author,Book

def add_book(request):

    if request.method=="POST":
title=request.POST.get("title")
price=request.POST.get("price")
pub_date=request.POST.get("pub_date")
publish_id=request.POST.get("publish_id")
authors_id_list=request.POST.getlist("authors_id_list") # checkbox,select book_obj=Book.objects.create(title=title,price=price,publishDate=pub_date,publish_id=publish_id)
print(authors_id_list) # ['', ''] book_obj.authors.add(*authors_id_list)
return redirect("/books/") publish_list=Publish.objects.all()
author_list=Author.objects.all() return render(request,"addbook.html",{"author_list":author_list,"publish_list":publish_list}) def books(request): book_list=Book.objects.all()
return render(request,"books.html",{"book_list":book_list}) def change_book(request,edit_book_id):
edit_book_obj=Book.objects.filter(pk=edit_book_id).first() if request.method=="POST":
title=request.POST.get("title")
price=request.POST.get("price")
pub_date=request.POST.get("pub_date")
publish_id=request.POST.get("publish_id")
authors_id_list=request.POST.getlist("authors_id_list") # checkbox,select Book.objects.filter(pk=edit_book_id).update(title=title,price=price,
publishDate=pub_date,publish_id=publish_id)
# edit_book_obj.authors.clear()
# edit_book_obj.authors.add(*authors_id_list) edit_book_obj.authors.set(authors_id_list) return redirect("/books/") publish_list=Publish.objects.all()
author_list=Author.objects.all() return render(request,"editbook.html",{"edit_book_obj":edit_book_obj,"publish_list":publish_list,"author_list":author_list}) def delete_book(request,delete_book_id): Book.objects.filter(pk=delete_book_id).delete() return redirect("/books/") def layout(request): return render(request,'layout.html') def motai(request):
return render(request,'motai.html')

models.py

from django.db import models

# Create your models here.

class Author(models.Model):
nid = models.AutoField(primary_key=True)
name=models.CharField( max_length=)
age=models.IntegerField() class Publish(models.Model):
nid = models.AutoField(primary_key=True)
name=models.CharField( max_length=)
city=models.CharField( max_length=)
email=models.EmailField() class Book(models.Model): nid = models.AutoField(primary_key=True)
title = models.CharField( max_length=)
publishDate=models.DateField()
price=models.DecimalField(max_digits=,decimal_places=) # 999.99 # 与Publish建立一对多的关系,外键字段建立在多的一方
publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE)
# 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表
authors=models.ManyToManyField(to='Author',)

motai.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title> <style>
.hide{
display: none;
}
.shadow{
position: fixed;
top: ;
left: ;
right: ;
bottom: ;
background-color:black;
opacity: 0.4;
z-index: ;
}
.modal{
z-index: ;
position: fixed;
left: %;
top: %;
height: 300px;
width: 400px;
background-color: white;
margin-left: -200px;
margin-top: -150px;
}
</style>
</head>
<body>
<a onclick="showModal();" href="">对话框添加</a>
<div id="shadow" class="shadow hide"></div>
<div id="modal" class="modal hide">
<form action="" method="post">
<p>
<input type="text" name="title">
</p>
<input type="submit" value="提交">
</form>
</div>
<script>
function showModal() {
document.getElementById('shadow').classList.remove('hide');
document.getElementById('modal').classList.remove('hide'); } </script> </body>
</html>

layout.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/bs/css/bootstrap.css">
<link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="/static/css/commons.css">
{% block css %}{% endblock %}
</head>
<body> <div class="pg-header">
<div class="logo left">老女孩后台管理</div>
<div class="avator right">
<img src="/static/images/1.jpg" alt="">
<div class="user-info">
<a href="">个人资料</a>
<a href="">注销</a>
</div>
</div>
<div class="rmenus right">
<a href=""><i class="fa fa-commenting-o" aria-hidden="true"></i> 消息</a>
<a href=""><i class="fa fa-envelope-o" aria-hidden="true"></i> 邮件</a> </div> </div>
<div class="pg-body">
<div class="menus">
<a href=""><i class="fa fa-bug" aria-hidden="true"></i> 班级管理</a>
<a href=""><i class="fa fa-blind" aria-hidden="true"></i> 学生管理</a>
<a href=""><i class="fa fa-flag" aria-hidden="true"></i> 教师管理</a> </div>
<div class="content">
<ol class="breadcrumb">
<li><a href="#">首页</a></li>
<li><a href="#">班级管理</a></li>
<li class="active">添加班级</li>
</ol>
<div>
<div class="row">
<div class="col-md-offset-2 col-md-8">
{% block content %}
{% endblock %} </div> </div> </div> <nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
</div> {% block js %} {% endblock %}
</body>
</html>

addbook.html

{% extends 'layout.html' %}

{% block content %}
<h3>增加书籍</h3>
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="">名称</label>
<input type="text" name="title" class="form-control" value="">
</div> <div class="form-group">
<label for="">价格</label>
<input type="text" name="price" class="form-control">
</div> <div class="form-group">
<label for="">出版日期</label>
<input type="date" name="pub_date" class="form-control">
</div> <div class="form-group">
<label for="">出版社</label>
<select name="publish_id" id="" class="form-control">
{% for publish in publish_list %}
<option value="{{ publish.pk }}">{{ publish.name }}</option>
{% endfor %}
</select>
</div> <div class="form-group">
<label for="">作者</label>
<select type="text" name="authors_id_list" multiple class="form-control">
{% for author in author_list %}
<option value="{{ author.pk }}">{{ author.name }}</option>
{% endfor %} </select>
</div>
<input type="submit" class="btn btn-default"> </form>
{% endblock %}

books.html

{% extends 'layout.html' %}

{% block css %}

{% endblock %}

{% block content %}
<a href="/books/add/" class="fa fa-handshake-o">添加书籍</a>
<table class="table table-bordered table-hover table-striped"> <thead>
<tr>
<th>编号</th>
<th>书籍名称</th>
<th>价格</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th> </tr>
</thead>
<tbody>
{% for book in book_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book.title }}</td>
<td>{{ book.price }}</td>
<td>{{ book.publishDate|date:"Y-m-d" }}</td>
<td>
{{ book.publish.name }}
</td>
<td>
{% for author in book.authors.all %}
{% if forloop.last %}
<span>{{ author.name }}</span>
{% else %}
<span>{{ author.name }}</span>,
{% endif %} {% endfor %} </td> <td>
<a href="/books/{{ book.pk }}/change/" class="glyphicon glyphicon-pencil">编辑</a>
<a href="/books/{{ book.pk }}/delete/" class="glyphicon glyphicon-trash">删除</a>
</td> </tr>
{% endfor %} </tbody>
</table>
{% endblock %} {% block js %} {% endblock %} <!--<!DOCTYPE html>-->
<!--<html lang="en">-->
<!--<head>-->
<!--<meta charset="UTF-8">-->
<!--<title>Title</title>-->
<!--<link rel="stylesheet" href="/static/bs/css/bootstrap.css">-->
<!--<link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.css">--> <!--</head>-->
<!--<body>--> <!--<h3>查看书籍</h3>--> <!--<div class="container">-->
<!--<div class="row">-->
<!--<div class="col-md-8 col-md-offset-2">--> <!--<a href="/books/add/" class="fa fa-handshake-o"></a>-->
<!--<table class="table table-bordered table-hover table-striped">--> <!--<thead>-->
<!--<tr>-->
<!--<th>编号</th>-->
<!--<th>书籍名称</th>-->
<!--<th>价格</th>-->
<!--<th>出版日期</th>-->
<!--<th>出版社</th>-->
<!--<th>作者</th>-->
<!--<th>操作</th>--> <!--</tr>-->
<!--</thead>-->
<!--<tbody>-->
<!--{% for book in book_list %}-->
<!--<tr>-->
<!--<td>{{ forloop.counter }}</td>-->
<!--<td>{{ book.title }}</td>-->
<!--<td>{{ book.price }}</td>-->
<!--<td>{{ book.publishDate|date:"Y-m-d" }}</td>-->
<!--<td>-->
<!--{{ book.publish.name }}-->
<!--</td>-->
<!--<td>-->
<!--{% for author in book.authors.all %}-->
<!--{% if forloop.last %}-->
<!--<span>{{ author.name }}</span>-->
<!--{% else %}-->
<!--<span>{{ author.name }}</span>,-->
<!--{% endif %}--> <!--{% endfor %}--> <!--</td>--> <!--<td>-->
<!--<a href="/books/{{ book.pk }}/change/" class="glyphicon glyphicon-pencil"></a>-->
<!--<a href="/books/{{ book.pk }}/delete/" class="glyphicon glyphicon-trash"></a>-->
<!--</td>--> <!--</tr>-->
<!--{% endfor %}--> <!--</tbody>-->
<!--</table>-->
<!--</div>-->
<!--</div>-->
<!--</div>--> <!--&lt;!&ndash; Button trigger modal &ndash;&gt;-->
<!--<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">-->
<!--Launch demo modal-->
<!--</button>-->
<!--<div class="modal fade" tabindex="-1" role="dialog">-->
<!--<div class="modal-dialog" role="document">-->
<!--<div class="modal-content">-->
<!--<div class="modal-header">-->
<!--<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>-->
<!--<h4 class="modal-title">Modal title</h4>-->
<!--</div>-->
<!--<div class="modal-body">-->
<!--<p>One fine body&hellip;</p>-->
<!--</div>-->
<!--<div class="modal-footer">-->
<!--<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>-->
<!--<button type="button" class="btn btn-primary">Save changes</button>-->
<!--</div>-->
<!--</div>&lt;!&ndash; /.modal-content &ndash;&gt;-->
<!--</div>&lt;!&ndash; /.modal-dialog &ndash;&gt;-->
<!--</div>&lt;!&ndash; /.modal &ndash;&gt;--> <!--</body>-->
<!--</html>-->

editbook.html

{% extends 'layout.html' %}

{% block content %}
<h3>编辑书籍</h3>
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="">名称</label>
<input type="text" name="title" class="form-control" value="{{ edit_book_obj.title }}">
</div> <div class="form-group">
<label for="">价格</label>
<input type="text" name="price" class="form-control" value="{{ edit_book_obj.price }}">
</div> <div class="form-group">
<label for="">出版日期</label>
<input type="date" name="pub_date" class="form-control"
value="{{ edit_book_obj.publishDate|date:'Y-m-d' }}">
</div> <div class="form-group">
<label for="">出版社</label>
<select name="publish_id" id="" class="form-control">
{% for publish in publish_list %}
{% if edit_book_obj.publish == publish %}
<option selected value="{{ publish.pk }}">{{ publish.name }}</option>
{% else %}
<option value="{{ publish.pk }}">{{ publish.name }}</option>
{% endif %}
{% endfor %}
</select>
</div> <div class="form-group">
<label for="">作者</label>
<select type="text" name="authors_id_list" multiple class="form-control">
{% for author in author_list %} {% if author in edit_book_obj.authors.all %}
<option selected value="{{ author.pk }}">{{ author.name }}</option>
{% else %}
<option value="{{ author.pk }}">{{ author.name }}</option>
{% endif %} {% endfor %}
</select>
</div>
<input type="submit" class="btn btn-default"> </form> {% endblock %}

Django book manage system的更多相关文章

  1. Django笔记 manage.py脚本的使用

    1. 管理Django项目 python manage.py startproject projectname # 新建Django project ~~projectname是自己的项目名称 pyt ...

  2. Django使用manage.py备份与恢复数据

    Django dumpdata and loaddata django database model dumpdata dumpdata command It is a django manageme ...

  3. No module named _sqlite3 django python manage.py runserver

    linux 执行django(python manage.py runserver),报错No module named _sqlite3,需要安装sqlite-devel,再重新编译安装python ...

  4. python后台架构Django教程——manage.py命令

    一.manage.py命令选项 manage.py是每个Django项目中自动生成的一个用于管理项目的脚本文件,需要通过python命令执行.manage.py接受的是Django提供的内置命令. 内 ...

  5. Django 执行 manage 命令方式

    本人使用的Pycharm作为开发工具,可以在顶部菜单栏的Tools->Run manage.py Task直接打开manager 命令控制台 打开后在底部会有命令窗口: 或者,也可以在Pytho ...

  6. django python manage.py runserver 流程

    python manage.py runserver 流程分析 版本 python27 django 1.0 搭建可运行的环境 创建python27 虚拟环境 github 下载 django-1.0 ...

  7. 关于django python manage.py startapp 应用名 出错异常原因

    如题,在控制台运行python manage.py startapp sales 建立一个应用报错异常 1.应用名不能包含下划线等字符 所以app-demo 不能作为应用名被定义 2.manage.p ...

  8. 使用django-extension扩展django的manage――runscript命令

    摘要:1.下载安装   1)$easy_installdjango-extensions   2)在INSTALLED_APP中添加'django_extensions'[python]INSTALL ...

  9. Django中manage.py migrate无效的问题

    在改变Django-model中结构后,makemigrations可以识别到改变但migrate没有操作,数据库中表结构也没有改变,原因如下: 在由Django-model自动生成的数据库表中有名为 ...

随机推荐

  1. linux下/proc/diskstats文件详解

    每一列的含义分别为: 第一列为 设备号 (number of issued reads. This is the total number of reads completed successfull ...

  2. 逆袭之旅DAY09.东软实训.接口

    2018年7月5日 package day0705.teacher.test1usb; /** * 测试类 * @author Administrator * */ public class UsbI ...

  3. Tomcat错误:getOutputStream() has already been called for this response

    使用weblogic部署时,没有报错.客户现场使用tomcat后报错. 在tomcat下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因.具体的原 ...

  4. day19-python的正则表达式2

    正则对象的findall方法 findall(string[, pos[, endpos]])  搜索string,以列表形式返回全部能匹配的子串. import re p1 = re.compile ...

  5. getopts的使用方法

    getopts的使用 语法格式:getopts [option[:]] [DESCPRITION] VARIABLE option:表示为某个脚本可以使用的选项 ":":如果某个选 ...

  6. Oracle 11gR2 Database UNDO表空间使用率居高不下处理

    一.UNDO表空间监控图 Prometheus监控的到UNDO表空间使用率超过90%(90%为所有表空间告警阈值).从图中可以看到,多次增加UNDO表空间的DATAFILE,UNDO表空间达到40GB ...

  7. web service基础知识

    Web服务基础 用户访问网站的基本流程 我们每天都会用web客户端上网,浏览器就是一个web客户端,例如谷歌浏览器,以及火狐浏览器等. 当我们输入www.oldboyedu.com/时候,很快就能看到 ...

  8. Java数值类型之间转换

    Java之间的数值转换如图所示,实心箭头代表无数据丢失,虚线箭头代表可能丢失 例如:123456789是一个大的整数,包含的位数比float类型能够表达的位数多,但这个数转换为float类型时,将会得 ...

  9. Batch Normalization 引出的一系列问题

    Batch Normalization,拆开来看,第一个单词意思是批,出现在梯度下降的概念里,第二个单词意思是标准化,出现在数据预处理的概念里. 我们先来看看这两个概念. 数据预处理 方法很多,后面我 ...

  10. php优秀框架codeigniter学习系列——前言

    php的框架众多,笔者用过的包括thinkphp,CI,smarty,laravel,也用过一些公司自己开发的框架. thinkphp是国人自己开发的,我大概用过一段时间,基本功能都还好,应该也还比较 ...