Django book manage system
创建一个简易的可以增删改查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">«</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">»</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>-->
<!--<!– Button trigger modal –>-->
<!--<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">×</span></button>-->
<!--<h4 class="modal-title">Modal title</h4>-->
<!--</div>-->
<!--<div class="modal-body">-->
<!--<p>One fine body…</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><!– /.modal-content –>-->
<!--</div><!– /.modal-dialog –>-->
<!--</div><!– /.modal –>-->
<!--</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的更多相关文章
- Django笔记 manage.py脚本的使用
1. 管理Django项目 python manage.py startproject projectname # 新建Django project ~~projectname是自己的项目名称 pyt ...
- Django使用manage.py备份与恢复数据
Django dumpdata and loaddata django database model dumpdata dumpdata command It is a django manageme ...
- No module named _sqlite3 django python manage.py runserver
linux 执行django(python manage.py runserver),报错No module named _sqlite3,需要安装sqlite-devel,再重新编译安装python ...
- python后台架构Django教程——manage.py命令
一.manage.py命令选项 manage.py是每个Django项目中自动生成的一个用于管理项目的脚本文件,需要通过python命令执行.manage.py接受的是Django提供的内置命令. 内 ...
- Django 执行 manage 命令方式
本人使用的Pycharm作为开发工具,可以在顶部菜单栏的Tools->Run manage.py Task直接打开manager 命令控制台 打开后在底部会有命令窗口: 或者,也可以在Pytho ...
- django python manage.py runserver 流程
python manage.py runserver 流程分析 版本 python27 django 1.0 搭建可运行的环境 创建python27 虚拟环境 github 下载 django-1.0 ...
- 关于django python manage.py startapp 应用名 出错异常原因
如题,在控制台运行python manage.py startapp sales 建立一个应用报错异常 1.应用名不能包含下划线等字符 所以app-demo 不能作为应用名被定义 2.manage.p ...
- 使用django-extension扩展django的manage――runscript命令
摘要:1.下载安装 1)$easy_installdjango-extensions 2)在INSTALLED_APP中添加'django_extensions'[python]INSTALL ...
- Django中manage.py migrate无效的问题
在改变Django-model中结构后,makemigrations可以识别到改变但migrate没有操作,数据库中表结构也没有改变,原因如下: 在由Django-model自动生成的数据库表中有名为 ...
随机推荐
- 蓝桥杯—ALGO-2 最小最大公倍数
问题描述已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少. 输入格式输入一个正整数N. 输出格式输出一个整数,表示你找到的最小公倍数.样例输入9样例输出504数据规模与约定1 ...
- Android 音视频深入 四 录视频MP4(附源码下载)
本篇项目地址,名字是<录音视频(有的播放器不能放,而且没有时长显示)>,求star https://github.com/979451341/Audio-and-video-learnin ...
- 北邮新生排位赛2解题报告a-c
A. 丁神去谷歌 2014新生暑假个人排位赛02 时间限制 1000 ms 内存限制 65536 KB 题目描述 丁神要去Google上班了,去之前丁神想再做一道水题,但时间不多了,所以他希望题目做起 ...
- day14-python异常处理
1. 异常 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当Pyt ...
- MySQL 数据库备份策略:全备与增量备份
一.备份策略1.周日全备份,周一至周六增量备份2.全备份目录/u03/backup/innobackup/full_backup3.增量备份目录/u03/backup/innobackup/incre ...
- docker samba
这个就是匿名用户可以登录访问,不能写. root登录,就可以写了. #命令,是在物理机上运行的. 主要是根据dockerfile构建镜像. 启动容器 进入镜像 设置root密码. 附smb.conf ...
- EF-记录程序自动生成并执行的sql语句日志
在EntityFramework的CodeFirst模式中,我们想将程序自动生成的sql语句和执行过程记录到日志中,方便以后查看和分析. 在EF的6.x版本中,在DbContext中有一个Databa ...
- (C/C++学习笔记) 二十三. 运行时类型识别
二十三. 运行时类型识别 ● 定义 运行时类型识别(Run-time Type Identification, RTTI) 通过RTTI, 程序能够使用基类的指针或引用来检查(check)这些指针或引 ...
- C数据结构 : 线性表 与 链表
一.线性表 一般表现为数组,使用一组地址连续的存储单元依次存储数据元素,如图: 它具有如下特点: 长度固定,必须在分配内存之前确定数组的长度. 存储空间连续,即允许元素的随机访问. 存储密度大,内存中 ...
- ubuntu下修改文件权限
参考:https://www.cnblogs.com/viviwind/archive/2012/08/02/2619451.html 常用方法如下: sudo chmod 600 ××× (只有所有 ...