Django项目图书管理系统
一、创建Django项目
1、创建项目--------Library
2、创建并注册应用index
二、数据库配置
1、配置数据库信息
在配置文件settings.py里配置数据库信息
2、进行数据迁移
(1)创建数据库-----Lirbrary
(2)设置数据库连接模块
迁移之前先导入os模块
import os
(3)执行数据迁移命令
在控制台依次执行两条数据迁移命令,生成数据表
python manage.py makemigrations
python manage.py migrate
打开数据库,查看新生成的数据表
二、完成基本配置
1、路由配置
(1)主路由配置
配置主路由 - Library里的urls.py
(2)分路由配置
2、创建图书模型和用户信息类——Books——User
在index的models.py里创建图书模型和用户信息模型类
from django.db import models
# 创建图书模型
class Books(models.Model):
id = models.AutoField('编号', primary_key=True)
number = models.CharField('总编号', max_length=10)
category = models.CharField('分类号', max_length=10)
name = models.CharField('书名', max_length=50)
author = models.CharField('作者', max_length=10)
press = models.CharField('出版单位', max_length=50)
price = models.DecimalField('单价', decimal_places=2, max_digits=6)
def __str__(self):
return str(self.name)
# 用户信息类
class User(models.Model):
id = models.AutoField('编号', primary_key=True)
username = models.CharField('用户名', max_length=20)
password = models.CharField('密码', max_length=50)
last_Login_Time = models.DateTimeField('上次登录时间')
def __str__(self):
return str(self.username)
3、创建视图函数
在index的views.py里创建
from django import db
from django.http import HttpResponse
from django.shortcuts import render, redirect
from itsdangerous import json
from index.models import User, Books
# 创建登录视图函数
def loginView(request):
return render(request, 'login.html')
# 创建处理登录请求的视图函数
def do_loginView(request):
# 判断请求方式
if request.method == 'POST':
# 获取登录表单提交的数据
username = request.POST.get('username')
password = request.POST.get('password')
# 利用用户模型查询
users = User.objects.raw('select * from index_user where username= %s and password=%s',
params=[username, password])
# 判断用户是否登录成功
if len(users) > 0:
books = Books.objects.all()
return render(request, 'books.html', locals())
else:
login_msg = ['用户名或密码错误']
return render(request, 'login.html', dict(data=json.dumps(login_msg)))
# 创建显示图书视图函数
def booksView(request):
books = Books.objects.all()
return render(request, 'books.html', locals())
# 删除图书视图函数
def del_publisher(request):
id = request.GET.get('id')
Books.objects.filter(id=id).delete()
books = Books.objects.all()
return render(request, '../books.html', locals())
# 编辑图书视图
def ditbooks(request):
id = request.GET.get('id')
obj_list = Books.objects.filter(id=id)
if not obj_list:
return HttpResponse('要编辑的数据不存在')
obj = obj_list[0]
if request.method == 'POST':
books_id = request.POST.get('books_id')
books_number = request.POST.get('books_number')
books_category = request.POST.get('books_category')
books_name = request.POST.get('books_name')
books_author = request.POST.get('books_author')
books_press = request.POST.get('books_press')
books_price = request.POST.get('books_price')
obj.id = books_id
obj.number = books_number
obj.category = books_category
obj.name = books_name
obj.author = books_author
obj.press = books_press
obj.price = books_price
obj.save()
books = Books.objects.all()
return render(request, '../books.html', locals())
return render(request, '../dit.html', {'obj': obj})
在主路由文件里导入上述视图函数
4、做数据迁移,生成图书和用户表
在控制台依次执行两条命令
python manage.py makemigrations
python manage.py migrate
打开数据库,查看新生成的数据表
四、创建html页面
页面需求:
登录页面——login.html
图书显示及删除页面——books.html
编辑页面——dit.html
1、在index应用下创建一个templates目录,再在templates目录下创建一个index目录,用来存放html文件
2、编辑登录页面——login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form id="frmLogin" action="do_login" method="post">
{% csrf_token %}
<table class="tb" border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号</td>
<td><input id="username" type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input id="password" type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
<script language="JavaScript">
login_msg={{ data | safe }};
if (login_msg !=null){
alert(login_msg);
}
</script>
</form>
</body>
</html>
3、编辑图书显示及删除页面——books.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<h3 align="center">图书信息</h3>
<table border="1" cellpadding="7" align="center">
<tr>
<th>编号</th>
<th>总编号</th>
<th>分类号</th>
<th>书名</th>
<th>作者</th>
<th>出版单位</th>
<th>单价</th>
<th>操作</th>
</tr>
{% for book in books %}
<tr>
<td width="50" align="center">{{ book.id }}</td>
<td width="100" align="center">{{ book.number }}</td>
<td width="100" align="center">{{ book.category }}</td>
<td width="100" align="center">{{ book.name }}</td>
<td width="100" align="center">{{ book.author }}</td>
<td width="130" align="center">{{ book.press }}</td>
<td width="100" align="center">{{ book.price }}</td>
<td width="200" align="center"><a href="/delete/?id={{ book.id }}" target="_blank">删除</a>
<a href="/dit/?id={{ book.id }}" target="_blank">编辑</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
4、编辑页面——dit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编辑</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<p>编号:<input type="text" name="books_id" value="{{ obj.id}}"></p>
<p>总编号:<input type="text" name="books_number" value="{{ obj.number }}"></p>
<p>分类号:<input type="text" name="books_category" value="{{ obj.category }}"></p>
<p>书名:<input type="text" name="books_name" value="{{ obj.name }}"></p>
<p>作者:<input type="text" name="books_author" value="{{ obj.author }}"></p>
<p>出版单位:<input type="text" name="books_press" value="{{ obj.press }}"></p>
<p>单价:<input type="text" name="books_price" value="{{ obj.price }}"></p>
<button>提交</button>
</form>
</body>
</html>
5、编辑数据库
数据库中插入几条新数据
新数据生成
五、启动项目
运行http://127.0.0.1:8000
登录界面
本文含有隐藏内容,请 开通VIP 后查看