创建一个简易的可以增删改查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. Notation, First Definitions 转 http://brnt.eu/phd/node9.html

    LaTeX command Equivalent to Output style Remarks \textnormal{...} {\normalfont...} document font fam ...

  2. learning ddr tRP and tRP tRTP CL tRAS

    referce :https://blog.csdn.net/ghostyu/article/details/7728106 tRP(RAS Precharge Time): “内存行地址控制器预充电 ...

  3. PC/FORTH 判定

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  4. 图的深度优先遍历(DFS)和广度优先遍历(BFS)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  5. 《Python》线程池、携程

    一.线程池(concurrent.futures模块) #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 P ...

  6. 查看json数据更新情况

    #! python3 # -*- coding:utf8 -*- #主要为读取excel中接口地址,打开网页爬取url页面中数据,解析json,检查是否符合逻辑(正常) import requests ...

  7. block,inline-block,行内元素区别及浮动

    1.block: 默认占据一行空间,盒子外的元素被迫另起一行 2.inline-block: 行内块盒子,可以设置宽高 3.行内元素: 宽度即使内容宽度,不能设置宽高,如果要设置宽高,需要转换成行内块 ...

  8. Mysql使用information.shema.tables查询数据库表大小

    简介: information_schema数据库中的表都是只读的,不能进行更新.删除和插入等操作,也不能加触发器,因为它们实际只是一个视图,不是基本表,没有关联的文件. 元数据描述数据的数据,用于描 ...

  9. 3-D crustal model transfer to cdl format

    The downloaded crustal model file, for example, its name is TW-PS-H14.nc The command is ncdump -b c ...

  10. C#正则表达式类Match和Group类的理解

    正则表达式可以看做一种有特定功能的小型编程语言,在一段文本中定位子字符串.利用正则表达式可以快速地分析大量的文本以找到特定的字符模式:提取.编辑.替换或删除文本子字符串:或将提取的字符串添加到集合.正 ...