在线图书信息入库程序开发(二)

目的:集成mongoengine,添加查看图书列表、添加图书功能;

一、创建应用 在命令行中输入下面指令创建home应用 1

  python manage.py startapp home 

命令执行完毕后,会在项目目录中生成home文件夹,

在这里插入图片描述

二、安装mongoengine插件

打开命令行输入下面指令安装插件。 1

pip install mongoengine 

三、修改配置

打开book目录下的settings.py文件,
在这里插入图片描述
修改DATABASES选项,添加mongodb相关信息,
在这里插入图片描述
因为此处mongodb与当前程序运行在同一主机中并且数据库端口号默认为27017,此处只 用配置数据库名称即可。

四、编写数据模型

1 from django.db import models 
2 #导入mongoengine相关库 
3 from mongoengine import * 
4 #导入settings.py文件,下文中用来引用mongodb数据库的配置文件 
5 from book.settings import DATABASES 
6 from datetime import datetime 
7 #使用connect方法连接数据库 
8 connect(DATABASES['mongodb']['NAME']) 
9 #创建图书类,该类在数据库中会根据类名生成集合(book) 
10 class book(Document): 
11     isbn=StringField()#编号 
12     name=StringField()#名称 
13     author=StringField()#作者 
14     press=StringField()#出版社
15     date=DateTimeField(default=datetime.now(), required=True)#添加记录时 间 
16     kind=StringField()#分类

五、编写图书列表和添加图书功能

1)打开home目录中的view.py文件,添加代码如下:

 from django.shortcuts import render,HttpResponseRedirect 
 2 from .models import book
3 
4 def list(request):#显示图书列表 
5     books=book.objects.all()#查询所有数据 
6     return render(request,'list.html',{'books':books})
7 
8 def create(request,):#新增数据 
9     if request.method=='POST': 
10         isbn=request.POST.get("isbn") 
11         name=request.POST.get("name") 
12         author=request.POST.get("author") 
13         press=request.POST.get("press") 
14         kind=request.POST.get("kind") 
15         bk=book(isbn=isbn,name=name,author=author,press=press,kind=kind) 16         bk.save() 
17         return render(request,'create.html',{'state':1}) 
18     return render(request,'create.html') 

2)编写母版文件
在templates文件夹中新建base.html,编写代码如下:

1 <!DOCTYPE html> 
2 <html lang="en">
3 
4 <head> 
5     <meta charset="UTF‐8"> 
6     <meta name="viewport" content="width=device‐width, initialscale=1.0"> 
7     <title>在线图书信息入库系统</title> 
8     <link rel="stylesheet" href="https://cdn.staticfile.org/twitter‐boots trap/3.3.7/css/bootstrap.min.css"> 
9     <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"> </script> 
10     <script src="https://cdn.staticfile.org/twitter‐bootstrap/3.3.7/js/b ootstrap.min.js"></script> 
11     <style> 
12         html,
13         body {
 14             padding: 0; 
 15             margin: 0;
  16         } 
  17         #top { 
  18             height: 50px; 
  19             background‐color: cadetblue; 
  20             line‐height: 50px; 
  21         }
22 
23         #content { 
24             width: 80%; 
25             margin: auto; 
26             margin‐top: 30px; 
27         } 
28     </style> 
29 </head>
30 
31 <body> 
32     <div id="top"> 
33         <span style="color:white;font‐size: 25px;margin‐left: 20px;">在线 图书信息入库程序</span> 
34     </div> 
35     <div id="content"> 
36        {%block content%} 
37        {%endblock%} 
38     </div> 
39 </body> 
40 </html>

3)编写列表页面
在templates文件夹中新建list.html,编写代码如下:

1 {%extends 'base.html'%}<!‐‐继承base.html文件‐‐> 
2 {%block content%} 
3 <div style="display: flex;flex‐direction: row;"> 
4     <a type="button" class="btn btn‐primary" href="/create"> <span class="   glyphicon glyphicon‐plus" 
5             style="margin‐right:5px;"></span>新增图书</a> 
6     <input type="text" class="form‐control" style="margin‐left: 20px;marg in‐right: 10px;"> 
7     <button class="btn btn‐default" type="button">查询</button>
8 </div> 
9 <table class="table table‐striped"> 
10     <thead> 
11         <tr> 
12             <th>序号</th>
 13             <th>ISBN</th> 
 14             <th>书名</th> 
 15             <th>作者</th> 
 16             <th>出版社</th> 
 17             <th>出版日期</th> 
 18             <th>图书类型</th> 
 19             <th>添加时间</th> 
 20             <th>操作</th> 
 21         </tr> 
 22     </thead> 
 23     <tbody> 
 24         {%for book in books%} 
 25         <tr> 
 26             <td>{{forloop.counter}}</td> 
 27             <td>{{book.name}}</td> 
 28             <td>{{book.author}}</td> 
 29             <td>{{book.press}}</td> 
 30             <td>560001</td>
  31             <td>{{book.kind}}</td>
   32             <td>{{book.date}}</td> 
   33             <td> <button type="button" class="btn btn‐success btn‐sm">修 改</button> 
   34                 <a type="button" class="btn btn‐danger btn‐sm" href="/de lete?isbn={{book.isbn}}">删除</a></td>
    35         </tr> 
   36         {%endfor%} 
   37     </tbody> 
   38 </table> 
   39 {%endblock%} 

六、添加访问路由

打开book目录中的urls.py文件,添加路由配置如下:
在这里插入图片描述

七、运行程序

在控制台中输入

python manage.py runserver

运行项目,
打开浏览器输入

 localhost:8000,

查看界面如下图所示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

粉尘伴终生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值