您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
一、图书管理系统增删改查
导入bootstrap的三种方式:
1、首先在django项目中的settings.py中设置:
STATIC_URL = '/static/' STATICFILES_DIRS=[ os.path.join(BASE_DIR,'static') ]
2、在html文件中的三种方式:bootstrap依赖与jquery,所以jquery要写在bootstrap前面
方式一:
<head> <meta charset="UTF-8"> <script src="/static/jquery-3.3.1.js"></script> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> </head>
方式二:
<head> <meta charset="UTF-8"> <script src="/static/jquery-3.3.1.js"></script> {% load static %} <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.css' %}"> </head>
方式三:
<head> <meta charset="UTF-8"> <script src="/static/jquery-3.3.1.js"></script> {% load static %} <link rel="stylesheet" href="{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css"> </head>
urls.py
from django.conf.urls import url from django.contrib import admin from app01 import views # from app01.views import book_delete urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.book_list), url(r'^book_list/$', views.book_list,name='book_list'), url(r'^book_add/$', views.book_add), url(r'^author_add/$', views.author_add), url(r'^publish_add/$', views.publish_add), url(r'^author_list/$', views.author_list), url(r'^publish_list/$', views.publish_list), url(r'^book_delete/(?P<id>\d+)$',views.book_delete,name='book_delete'), url(r'^author_delete/$',views.author_delete), url(r'^author_update/(\d+)$',views.author_update), # url(r'^publish_delete/(?P<id>\d+)$',views.publish_delete), url(r'^publish_delete/$',views.publish_delete), url(r'^book_update/(?P<id>\d+)$',views.book_update), url(r'^publish_update/(\d+)$',views.publish_update), url(r'',views.error), ]
views.py
from django.shortcuts import render, redirect, reverse, HttpResponse # Create your views here. from app01 import models def book_list(request): book_list = models.Book.objects.all() return render(request, 'book_list.html', {"book_list": book_list}) def book_update(request, id): if request.method == "GET": book = models.Book.objects.get(pk=id) authors = models.Author.objects.all() publish_list = models.Publish.objects.all() return render(request, 'book_update.html', locals()) else: name = request.POST.get('name') price = request.POST.get('price') pub_date = request.POST.get('pub_date') authors = request.POST.getlist('authors') publish = request.POST.get('publish') # 第一种方式 book = models.Book.objects.get(pk=id) book.name = name book.price = price book.pub_date = pub_date book.publish_id = publish book.save() # 第二种方式。修改作者 book.authors.clear() book.authors.add(*authors) return redirect('/book_list/') def book_delete(request, id): try: models.Book.objects.get(pk=id).delete() except Exception as e: print(e) url = reverse('book_list') return redirect(url) def book_add(request): if request.method == "GET": authors = models.Author.objects.all() publish_list = models.Publish.objects.all() return render(request, 'book_add.html', locals()) else: name = request.POST.get('name') price = request.POST.get('price') pub_date = request.POST.get('pub_date') authors = request.POST.getlist('authors') publish = request.POST.get('publish') book = models.Book.objects.create(name=name, price=price, pub_date=pub_date, publish_id=publish) # 创建书跟作者的关联关系 book.authors.add(*authors) return redirect('/book_list/') def author_list(request): authors = models.Author.objects.all() return render(request, 'author_list.html', {'author_list': authors}) def publish_list(request): publishs = models.Publish.objects.all() return render(request, 'publish_list.html', {'publish_list': publishs}) def author_delete(request): id = request.GET.get('id') ret = models.Author.objects.filter(pk=id).delete() return redirect('/author_list/') def publish_delete(request,*args,**kwargs): # print(args,kwargs) id = request.GET.get('id') # print(id) # ret = models.Publish.objects.filter(pk=kwargs["id"]).delete() ret = models.Publish.objects.filter(pk=id).delete() return redirect('/publish_list/') def author_update(request, id): if request.method == "GET": author = models.Author.objects.get(pk=id) return render(request, 'author_update.html', locals()) else: name = request.POST.get('name') sex = request.POST.get('sex') models.Author.objects.filter(pk=id).update(name=name, sex=sex) return redirect('/author_list/') def publish_update(request,id): if request.method=="GET": publish=models.Publish.objects.get(pk=id) return render(request,"publish_update.html",locals()) else: name=request.POST.get("name") addr=request.POST.get("addr") models.Publish.objects.filter(pk=id).update(name=name,addr=addr) return redirect('/publish_list/') def author_add(request): if request.method=="GET": return render(request,"author_add.html") else: name=request.POST.get('name') sex=request.POST.get('sex') models.Author.objects.create(name=name,sex=sex) return redirect('/author_list/') def publish_add(request): if request.method=="GET": return render(request,"publish_add.html") else: name=request.POST.get("name") addr=request.POST.get("addr") models.Publish.objects.create(name=name,addr=addr) return redirect('/publish_list/') def error(request): return HttpResponse('404')
models.py
from django.db import models # Create your models here. #用了OneToOneField和ForeignKey,模型表的字段,后面会自定加_id # ManyToManyField会自动创建第三张表 # *************重点 # 一对一的关系:OneToOneField # 一对多的关系:ForeignKey # 多对多的关系:ManyToManyField class Publish(models.Model): # id如果不写,会自动生成,名字叫nid,并且自增(不同的数据库,叫的名称也不一样) id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) addr = models.CharField(max_length=64) email = models.EmailField() class Author(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) # 数字类型 sex = models.CharField(max_length=32) # 可以用ForeignKey,但是得设置唯一性约束,会报警告,不建议用,建议用OneToOneField # authordetail=models.ForeignKey(unique=True) # to='AuthorDetail' 加引号,这个表能找到就可以,不用引号,类必须在上面定义 authordetail = models.OneToOneField(to='AuthorDetail', to_field='id',null=True) def __str__(self): return self.name class AuthorDetail(models.Model): id = models.AutoField(primary_key=True) phone = models.CharField(max_length=32) addr = models.CharField(max_length=64) class Book(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32,db_index=True) price = models.DecimalField(max_digits=5, decimal_places=2) pub_date=models.DateField(auto_now_add=True) publish = models.ForeignKey(to=Publish, to_field='id',on_delete=models.CASCADE) authors = models.ManyToManyField(to=Author) def __str__(self): return self.name
templates/author_add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <title>新增作者</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h2>新增作者</h2> <hr> <form action="" method="post"> <div class="form-group"> <p>作者名字:<input type="text" name="name" class="form-control"></p> </div> <div class="form-group"> <p>性别:<input type="text" name="sex" class="form-control"></p> </div> <input type="submit" value="提交" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
templates/author_list.html
{% extends 'base.html' %} {% block content %} <script> $('.book').removeClass('active') $('.author').addClass('active') $('.publish').removeClass('active') </script> <div class="panel panel-primary"> <div class="panel-heading">作者列表</div> <div class="panel-body"> <a href="/author_add/" class="btn btn-primary pull-right">新增</a> <table class="table table-striped"> <thead> <tr> <th>id</th> <th>作者名</th> <th>性别</th> </tr> </thead> <tbody> {% for author in author_list %} <tr> <td>{{ forloop.counter }}</td> <td>{{ author.name }}</td> <td>{{ author.sex }}</td> <td><a href="/author_update/{{ author.pk }}">编辑</a></td> <td><a href="/author_delete/?id={{ author.pk }}">删除</a></td> </tr> {% endfor %} </tbody> </table> </div> <div class="panel-footer"> <nav aria-label="Page navigation"> <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="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </div> </div> {% endblock %}
templates/author_update.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <title>修改作者</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h2>修改作者</h2> <hr> <form action="/author_update/{{ author.pk }}" method="post"> <div class="form-group"> <p>作者姓名:<input type="text" name="name" class="form-control" value="{{ author.name }}"></p> </div> <div class="form-group"> <p>性别:<input type="text" name="sex" class="form-control" value="{{ author.sex }}"></p> </div> <input type="submit" value="提交" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
templates/base.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="/static/jquery-3.3.1.js"></script> {# <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">#} {% load static %} {# <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.css' %}">#} <link rel="stylesheet" href="{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css"> <title>图书列表</title> </head> <body> <div> <nav class="navbar navbar-default navbar-inverse"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <a class="navbar-brand" href="#">图书管理系统</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 navbar-right"> <li><a href="#">Link</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> </ul> </li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> </div> <div class="container-fluid"> <div class="row"> <div class="col-md-3"> <div class="list-group"> <a href="/book_list/" class="list-group-item active book"> 图书列表 </a> <a href="/author_list/" class="list-group-item author">作者列表</a> <a href="/publish_list/" class="list-group-item publish">出版社列表</a> </div> </div> <div class="col-md-9"> {% block content %} {% endblock %} </div> </div> </div> </body> </html>
templates/book_add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <title>新增图书</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h2>新增图书</h2> <hr> <form action="" method="post"> <div class="form-group"> <p>书名:<input type="text" name="name" class="form-control"></p> </div> <div class="form-group"> <p>价格:<input type="text" name="price" class="form-control"></p> </div> <div class="form-group"> <p>出版时间:<input type="date" name="pub_date" class="form-control"></p> </div> <div class="form-group"> <p>出版社: {# <select name="publish">#} {# <option value="1">北京出版社</option>#} {# <option value="2">上海出版社</option>#} {# <option value="3">南京出版社</option>#} {# </select>#} <select name="publish" class="form-control"> {% for publish in publish_list %} <option value="{{ publish.id }}">{{ publish.name }}</option> {% endfor %} </select> </p> </div> <div class="form-group"> <p>作者: {# <select name="authors">#} {# <option value="1">曹雪芹</option>#} {# <option value="2">吴承恩</option>#} {# <option value="3">曹雪芹</option>#} {# </select>#} <select name="authors" class="form-control" multiple> {% for author in authors %} <option value="{{ author.id }}">{{ author.name }}</option> {% endfor %} </select> </p> </div> <input type="submit" value="提交" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
templates/book_list.html
{% extends 'base.html' %} {% block content %} <div class="panel panel-primary"> <div class="panel-heading"> 图书列表</div> <div class="panel-body"> <a href="/book_add/" class="btn btn-primary pull-right">新增</a> <table class="table table-striped"> <thead> <tr> <th>id</th> <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.name }}</td> <td>{{ book.price }}</td> <td>{{ book.pub_date | date:'Y-m-d' }}</td> <td> {% for author in book.authors.all %} {% if forloop.last %} {{ author.name }} {% else %} {{ author.name }}| {% endif %} {% endfor %} </td> <td>{{ book.publish.name }}</td> <td><a href="/book_update/{{ book.pk }}">编辑</a></td> <td><a href="/book_delete/{{ book.pk }}">删除</a></td> </tr> {% endfor %} </tbody> </table> </div> <div class="panel-footer"> <nav aria-label="Page navigation"> <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="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </div> </div> {% endblock %}
templates/book_update.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <title>修改图书</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h2>修改图书</h2> <hr> <form action="/book_update/{{ book.pk }}" method="post"> <div class="form-group"> <p>书名:<input type="text" name="name" class="form-control" value="{{ book.name }}"></p> </div> <div class="form-group"> <p>价格:<input type="text" name="price" class="form-control" value="{{ book.price }}"></p> </div> <div class="form-group"> <p>出版时间:<input type="date" name="pub_date" class="form-control" value="{{ book.pub_date | date:'Y-m-d'}}"></p> </div> <div class="form-group"> <p>出版社: <select name="publish" class="form-control"> {% for publish in publish_list %} {% if publish == book.publish %} <option value="{{ publish.id }}" selected>{{ publish.name }}</option> {% else %} <option value="{{ publish.id }}">{{ publish.name }}</option> {% endif %} {% endfor %} </select> </p> </div> <div class="form-group"> <p>作者: <select name="authors" class="form-control" multiple> {% for author in authors %} {% if author in book.authors.all %} <option value="{{ author.id }}" selected>{{ author.name }}</option> {% else %} <option value="{{ author.id }}">{{ author.name }}</option> {% endif %} {% endfor %} </select> </p> </div> <input type="submit" value="提交" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
templates/publish_add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <title>新增出版社</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h2>新增出版社</h2> <hr> <form action="" method="post"> <div class="form-group"> <p>出版社名称:<input type="text" name="name" class="form-control"></p> </div> <div class="form-group"> <p>出版社地址:<input type="text" name="addr" class="form-control"></p> </div> <input type="submit" value="提交" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
templates/publish_list.html
{% extends 'base.html' %} {% block content %} <script> $('.book').removeClass('active') $('.author').removeClass('active') $('.publish').addClass('active') </script> <div class="panel panel-primary"> <div class="panel-heading"> 出版社列表</div> <div class="panel-body"> <a href="/publish_add/" class="btn btn-primary pull-right">新增</a> <table class="table table-striped"> <thead> <tr> <th>id</th> <th>出版社名称</th> <th>出版社地址</th> </tr> </thead> <tbody> {% for publish in publish_list %} <tr> <td>{{ forloop.counter }}</td> <td>{{ publish.name }}</td> <td>{{ publish.addr }}</td> <td><a href="/publish_update/{{ publish.pk }}">编辑</a></td> <td><a href="/publish_delete/?id={{ publish.pk }}">删除</a></td> </tr> {% endfor %} </tbody> </table> </div> <div class="panel-footer"> <nav aria-label="Page navigation"> <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="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </div> </div> {% endblock %}
templates/publish_update.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <title>修改出版社</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h2>修改出版社</h2> <hr> <form action="/publish_update/{{ publish.pk }}" method="post"> <div class="form-group"> <p>出版社名称:<input type="text" name="name" class="form-control" value="{{ publish.name }}"></p> </div> <div class="form-group"> <p>出版社地址:<input type="text" name="addr" class="form-control" value="{{ publish.addr }}"></p> </div> <input type="submit" value="提交" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。