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自动生成的数据库表中有名为 ...
随机推荐
- laravel本地开发的项目部署到服务器上
不建议用ftp,建议用git.svn等工具从你的开发服务器上拉取.或者使用一些自动构建发布的工具.如果非要用ftp上传,建议用以下步骤: 服务器上安装composer,并使用`composer sel ...
- java的八大排序
public class Sort2 { public static void main(String[] args) { Sort2 sort = new Sort2(); System.out.p ...
- js数组及常用数学方法
数组方法 清空数组 1: arr.length=0; 2: arr=[]; arr.push() //往数组最后一个添加元素,会待会一个返回值,就是新的数组长度arr.uns ...
- 学习Linux系统的方法有很多,适合自己的才是最好
综观现在互联网+的发展以及应用,作为一个从事IT行业的人员,应该懂得Linux占比多大份量,所以说,去掌握Linux是一种相当重要的谋生途径,当然,如果你对它产生情感那更好.这篇文章主要是本人结合自身 ...
- C/C++知识补充 (1)
● C++的圆括号运算符() 下列关于圆括号运算符的功能说法不正确的是(C) . A. 可用于强制类型转换 B 可用于类型构造 C 可用于类型声明 D 可用于函数调用 对大部分可重载的运算符来说,它既 ...
- CAN总线(1)--初探(更新中)
前言: CAN总线可以控制可以使用Xilinx中IP核来直接实现,也可以使用专用的CAN芯片(例如:SJA1000)通过单片机和FPGA驱动控制来实现: 目前是使用控制器SJA1000来进行实现: C ...
- DevExpress WPF v18.2新版亮点(二)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- 关于ArrayList中的iterator返回的事迭代器实例问题。
Arraylist是一个具体的类,它并没有定义它自己的iterator()方法,,它只是从AbstractList 这个抽象类中继承了iterator()这个方法,而AbstractList 中的it ...
- <Spark><Programming><Loading and Saving Your Data>
Motivation Spark是基于Hadoop可用的生态系统构建的,因此Spark可以通过Hadoop MapReduce的InputFormat和OutputFormat接口存取数据. Spar ...
- python scrapy爬虫数据库去重方法
1. scrapy对request的URL去重 yield scrapy.Request(url, self.parse, dont_filter=False) 注意这个参数:dont_filter= ...