一、图书管理系统
一、图书管理系统增删改查
导入bootstrap的三种方式:
1、首先在django项目中的settings.py中设置:
STATIC_URL='/static/'STATICFILES_DIRS=[os.path.join(BASE_DIR,'static')]
2、在html文件中的三种方式:bootstrap依赖与jquery,所以jquery要写在bootstrap前面
方式一:
<head><metacharset="UTF-8"><scriptsrc="/static/jquery-3.3.1.js"></script><linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"></head>
方式二:
<head><metacharset="UTF-8"><scriptsrc="/static/jquery-3.3.1.js"></script>{%loadstatic%}<linkrel="stylesheet"href="{%static'bootstrap-3.3.7-dist/css/bootstrap.css'%}"></head>
方式三:
<head><metacharset="UTF-8"><scriptsrc="/static/jquery-3.3.1.js"></script>{%loadstatic%}<linkrel="stylesheet"href="{%get_static_prefix%}bootstrap-3.3.7-dist/css/bootstrap.css"></head>
urls.py
fromdjango.conf.urlsimporturlfromdjango.contribimportadminfromapp01importviews#fromapp01.viewsimportbook_deleteurlpatterns=[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
fromdjango.shortcutsimportrender,redirect,reverse,HttpResponse#Createyourviewshere.fromapp01importmodelsdefbook_list(request):book_list=models.Book.objects.all()returnrender(request,'book_list.html',{"book_list":book_list})defbook_update(request,id):ifrequest.method=="GET":book=models.Book.objects.get(pk=id)authors=models.Author.objects.all()publish_list=models.Publish.objects.all()returnrender(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=namebook.price=pricebook.pub_date=pub_datebook.publish_id=publishbook.save()#第二种方式。修改作者book.authors.clear()book.authors.add(*authors)returnredirect('/book_list/')defbook_delete(request,id):try:models.Book.objects.get(pk=id).delete()exceptExceptionase:print(e)url=reverse('book_list')returnredirect(url)defbook_add(request):ifrequest.method=="GET":authors=models.Author.objects.all()publish_list=models.Publish.objects.all()returnrender(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)returnredirect('/book_list/')defauthor_list(request):authors=models.Author.objects.all()returnrender(request,'author_list.html',{'author_list':authors})defpublish_list(request):publishs=models.Publish.objects.all()returnrender(request,'publish_list.html',{'publish_list':publishs})defauthor_delete(request):id=request.GET.get('id')ret=models.Author.objects.filter(pk=id).delete()returnredirect('/author_list/')defpublish_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()returnredirect('/publish_list/')defauthor_update(request,id):ifrequest.method=="GET":author=models.Author.objects.get(pk=id)returnrender(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)returnredirect('/author_list/')defpublish_update(request,id):ifrequest.method=="GET":publish=models.Publish.objects.get(pk=id)returnrender(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)returnredirect('/publish_list/')defauthor_add(request):ifrequest.method=="GET":returnrender(request,"author_add.html")else:name=request.POST.get('name')sex=request.POST.get('sex')models.Author.objects.create(name=name,sex=sex)returnredirect('/author_list/')defpublish_add(request):ifrequest.method=="GET":returnrender(request,"publish_add.html")else:name=request.POST.get("name")addr=request.POST.get("addr")models.Publish.objects.create(name=name,addr=addr)returnredirect('/publish_list/')deferror(request):returnHttpResponse('404')
models.py
fromdjango.dbimportmodels#Createyourmodelshere.#用了OneToOneField和ForeignKey,模型表的字段,后面会自定加_id#ManyToManyField会自动创建第三张表#*************重点#一对一的关系:OneToOneField#一对多的关系:ForeignKey#多对多的关系:ManyToManyFieldclassPublish(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()classAuthor(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):returnself.nameclassAuthorDetail(models.Model):id=models.AutoField(primary_key=True)phone=models.CharField(max_length=32)addr=models.CharField(max_length=64)classBook(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):returnself.name
templates/author_add.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"><title>新增作者</title></head><body><divclass="container-fluid"><divclass="row"><divclass="col-md-6col-md-offset-3"><h2>新增作者</h2><hr><formaction=""method="post"><divclass="form-group"><p>作者名字:<inputtype="text"name="name"class="form-control"></p></div><divclass="form-group"><p>性别:<inputtype="text"name="sex"class="form-control"></p></div><inputtype="submit"value="提交"class="btnbtn-primary"></form></div></div></div></body></html>
templates/author_list.html
{%extends'base.html'%}{%blockcontent%}<script>$('.book').removeClass('active')$('.author').addClass('active')$('.publish').removeClass('active')</script><divclass="panelpanel-primary"><divclass="panel-heading">作者列表</div><divclass="panel-body"><ahref="/author_add/"class="btnbtn-primarypull-right">新增</a><tableclass="tabletable-striped"><thead><tr><th>id</th><th>作者名</th><th>性别</th></tr></thead><tbody>{%forauthorinauthor_list%}<tr><td>{{forloop.counter}}</td><td>{{author.name}}</td><td>{{author.sex}}</td><td><ahref="/author_update/{{author.pk}}">编辑</a></td><td><ahref="/author_delete/?id={{author.pk}}">删除</a></td></tr>{%endfor%}</tbody></table></div><divclass="panel-footer"><navaria-label="Pagenavigation"><ulclass="pagination"><li><ahref="#"aria-label="Previous"><spanaria-hidden="true">«</span></a></li><li><ahref="#">1</a></li><li><ahref="#">2</a></li><li><ahref="#">3</a></li><li><ahref="#">4</a></li><li><ahref="#">5</a></li><li><ahref="#"aria-label="Next"><spanaria-hidden="true">»</span></a></li></ul></nav></div></div>{%endblock%}
templates/author_update.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"><title>修改作者</title></head><body><divclass="container-fluid"><divclass="row"><divclass="col-md-6col-md-offset-3"><h2>修改作者</h2><hr><formaction="/author_update/{{author.pk}}"method="post"><divclass="form-group"><p>作者姓名:<inputtype="text"name="name"class="form-control"value="{{author.name}}"></p></div><divclass="form-group"><p>性别:<inputtype="text"name="sex"class="form-control"value="{{author.sex}}"></p></div><inputtype="submit"value="提交"class="btnbtn-primary"></form></div></div></div></body></html>
templates/base.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><scriptsrc="/static/jquery-3.3.1.js"></script>{#<linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">#}{%loadstatic%}{#<linkrel="stylesheet"href="{%static'bootstrap-3.3.7-dist/css/bootstrap.css'%}">#}<linkrel="stylesheet"href="{%get_static_prefix%}bootstrap-3.3.7-dist/css/bootstrap.css"><title>图书列表</title></head><body><div><navclass="navbarnavbar-defaultnavbar-inverse"><divclass="container-fluid"><!--Brandandtogglegetgroupedforbettermobiledisplay--><divclass="navbar-header"><aclass="navbar-brand"href="#">图书管理系统</a></div><!--Collectthenavlinks,forms,andothercontentfortoggling--><divclass="collapsenavbar-collapse"id="bs-example-navbar-collapse-1"><ulclass="navnavbar-navnavbar-right"><li><ahref="#">Link</a></li><liclass="dropdown"><ahref="#"class="dropdown-toggle"data-toggle="dropdown"role="button"aria-haspopup="true"aria-expanded="false">Dropdown<spanclass="caret"></span></a><ulclass="dropdown-menu"><li><ahref="#">Action</a></li><li><ahref="#">Anotheraction</a></li><li><ahref="#">Somethingelsehere</a></li><lirole="separator"class="divider"></li><li><ahref="#">Separatedlink</a></li></ul></li></ul></div><!--/.navbar-collapse--></div><!--/.container-fluid--></nav></div><divclass="container-fluid"><divclass="row"><divclass="col-md-3"><divclass="list-group"><ahref="/book_list/"class="list-group-itemactivebook">图书列表</a><ahref="/author_list/"class="list-group-itemauthor">作者列表</a><ahref="/publish_list/"class="list-group-itempublish">出版社列表</a></div></div><divclass="col-md-9">{%blockcontent%}{%endblock%}</div></div></div></body></html>
templates/book_add.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"><title>新增图书</title></head><body><divclass="container-fluid"><divclass="row"><divclass="col-md-6col-md-offset-3"><h2>新增图书</h2><hr><formaction=""method="post"><divclass="form-group"><p>书名:<inputtype="text"name="name"class="form-control"></p></div><divclass="form-group"><p>价格:<inputtype="text"name="price"class="form-control"></p></div><divclass="form-group"><p>出版时间:<inputtype="date"name="pub_date"class="form-control"></p></div><divclass="form-group"><p>出版社:{#<selectname="publish">#}{#<optionvalue="1">北京出版社</option>#}{#<optionvalue="2">上海出版社</option>#}{#<optionvalue="3">南京出版社</option>#}{#</select>#}<selectname="publish"class="form-control">{%forpublishinpublish_list%}<optionvalue="{{publish.id}}">{{publish.name}}</option>{%endfor%}</select></p></div><divclass="form-group"><p>作者:{#<selectname="authors">#}{#<optionvalue="1">曹雪芹</option>#}{#<optionvalue="2">吴承恩</option>#}{#<optionvalue="3">曹雪芹</option>#}{#</select>#}<selectname="authors"class="form-control"multiple>{%forauthorinauthors%}<optionvalue="{{author.id}}">{{author.name}}</option>{%endfor%}</select></p></div><inputtype="submit"value="提交"class="btnbtn-primary"></form></div></div></div></body></html>
templates/book_list.html
{%extends'base.html'%}{%blockcontent%}<divclass="panelpanel-primary"><divclass="panel-heading">图书列表</div><divclass="panel-body"><ahref="/book_add/"class="btnbtn-primarypull-right">新增</a><tableclass="tabletable-striped"><thead><tr><th>id</th><th>书名</th><th>价格</th><th>出版日期</th><th>作者</th><th>出版社</th><th>编辑</th><th>删除</th></tr></thead><tbody>{%forbookinbook_list%}<tr><td>{{forloop.counter}}</td><td>{{book.name}}</td><td>{{book.price}}</td><td>{{book.pub_date|date:'Y-m-d'}}</td><td>{%forauthorinbook.authors.all%}{%ifforloop.last%}{{author.name}}{%else%}{{author.name}}|{%endif%}{%endfor%}</td><td>{{book.publish.name}}</td><td><ahref="/book_update/{{book.pk}}">编辑</a></td><td><ahref="/book_delete/{{book.pk}}">删除</a></td></tr>{%endfor%}</tbody></table></div><divclass="panel-footer"><navaria-label="Pagenavigation"><ulclass="pagination"><li><ahref="#"aria-label="Previous"><spanaria-hidden="true">«</span></a></li><li><ahref="#">1</a></li><li><ahref="#">2</a></li><li><ahref="#">3</a></li><li><ahref="#">4</a></li><li><ahref="#">5</a></li><li><ahref="#"aria-label="Next"><spanaria-hidden="true">»</span></a></li></ul></nav></div></div>{%endblock%}
templates/book_update.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"><title>修改图书</title></head><body><divclass="container-fluid"><divclass="row"><divclass="col-md-6col-md-offset-3"><h2>修改图书</h2><hr><formaction="/book_update/{{book.pk}}"method="post"><divclass="form-group"><p>书名:<inputtype="text"name="name"class="form-control"value="{{book.name}}"></p></div><divclass="form-group"><p>价格:<inputtype="text"name="price"class="form-control"value="{{book.price}}"></p></div><divclass="form-group"><p>出版时间:<inputtype="date"name="pub_date"class="form-control"value="{{book.pub_date|date:'Y-m-d'}}"></p></div><divclass="form-group"><p>出版社:<selectname="publish"class="form-control">{%forpublishinpublish_list%}{%ifpublish==book.publish%}<optionvalue="{{publish.id}}"selected>{{publish.name}}</option>{%else%}<optionvalue="{{publish.id}}">{{publish.name}}</option>{%endif%}{%endfor%}</select></p></div><divclass="form-group"><p>作者:<selectname="authors"class="form-control"multiple>{%forauthorinauthors%}{%ifauthorinbook.authors.all%}<optionvalue="{{author.id}}"selected>{{author.name}}</option>{%else%}<optionvalue="{{author.id}}">{{author.name}}</option>{%endif%}{%endfor%}</select></p></div><inputtype="submit"value="提交"class="btnbtn-primary"></form></div></div></div></body></html>
templates/publish_add.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"><title>新增出版社</title></head><body><divclass="container-fluid"><divclass="row"><divclass="col-md-6col-md-offset-3"><h2>新增出版社</h2><hr><formaction=""method="post"><divclass="form-group"><p>出版社名称:<inputtype="text"name="name"class="form-control"></p></div><divclass="form-group"><p>出版社地址:<inputtype="text"name="addr"class="form-control"></p></div><inputtype="submit"value="提交"class="btnbtn-primary"></form></div></div></div></body></html>
templates/publish_list.html
{%extends'base.html'%}{%blockcontent%}<script>$('.book').removeClass('active')$('.author').removeClass('active')$('.publish').addClass('active')</script><divclass="panelpanel-primary"><divclass="panel-heading">出版社列表</div><divclass="panel-body"><ahref="/publish_add/"class="btnbtn-primarypull-right">新增</a><tableclass="tabletable-striped"><thead><tr><th>id</th><th>出版社名称</th><th>出版社地址</th></tr></thead><tbody>{%forpublishinpublish_list%}<tr><td>{{forloop.counter}}</td><td>{{publish.name}}</td><td>{{publish.addr}}</td><td><ahref="/publish_update/{{publish.pk}}">编辑</a></td><td><ahref="/publish_delete/?id={{publish.pk}}">删除</a></td></tr>{%endfor%}</tbody></table></div><divclass="panel-footer"><navaria-label="Pagenavigation"><ulclass="pagination"><li><ahref="#"aria-label="Previous"><spanaria-hidden="true">«</span></a></li><li><ahref="#">1</a></li><li><ahref="#">2</a></li><li><ahref="#">3</a></li><li><ahref="#">4</a></li><li><ahref="#">5</a></li><li><ahref="#"aria-label="Next"><spanaria-hidden="true">»</span></a></li></ul></nav></div></div>{%endblock%}
templates/publish_update.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="stylesheet"href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"><title>修改出版社</title></head><body><divclass="container-fluid"><divclass="row"><divclass="col-md-6col-md-offset-3"><h2>修改出版社</h2><hr><formaction="/publish_update/{{publish.pk}}"method="post"><divclass="form-group"><p>出版社名称:<inputtype="text"name="name"class="form-control"value="{{publish.name}}"></p></div><divclass="form-group"><p>出版社地址:<inputtype="text"name="addr"class="form-control"value="{{publish.addr}}"></p></div><inputtype="submit"value="提交"class="btnbtn-primary"></form></div></div></div></body></html>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。