django搭建图书管理系统

简介: django搭建图书管理系统

Django是一个高效、灵活的Python Web框架,它可以快速地构建Web应用程序。在本篇文章中,我们将介绍如何使用Django框架构建一个图书管理系统。

1.使用虚拟环境创建项目

5bcb5722c3044631b5594198b322f4f0.png2.安装django模块,可使用代码 pip install django 进行安装,也可以在Pycharm的Python解释器下”+”安装


c35f6ca734c14bf9b7bea40d77a0e855.png

3.创建项目

django-admin startproject Library

4创建APP

cd Library
python manage.py startapp MyApp

5.设置setting.py文件

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'MyApp',
]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Library.urls'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'MyApp/templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
WSGI_APPLICATION = 'Library.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'Library',  # 数据库名字
        'USER': 'bd',  # 用户名
        'PASSWORD': '123456',  # 密码
        'HOST': '127.0.0.1',  # 本地主机
        'PORT': '3306'  # 端口号
    }
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
# STATIC_URL = '/static/'
# STATICFILES_DIRS = (os.path.join(BASE_DIR), 'static')
STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static')

6.Library/urls.py配置

rom django.contrib import admin
from django.urls import path, include
from MyApp import views as App_views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('MyApp/', include('MyApp.urls')),
    path('login/', App_views.login),
    path('student_register/', App_views.student_register),
    path('manager_register/', App_views.manager_register),
    path('login_judge/', App_views.login_judge),
    path('student_information/', App_views.student_information),
    path('search_book/', App_views.search_book),
    path('borrow_record/', App_views.borrow_record),
    path('change_password/', App_views.change_password),
    path('borrow_book/', App_views.borrow_book),
    path('return_book/', App_views.return_book),
    path('manager_information/', App_views.manager_information),
    path('manage_book/', App_views.manage_book),
    path('delete_book/', App_views.delete_book),
    path('add_book/', App_views.add_book),
    path('reduce_book/', App_views.reduce_book),
    path('change_manager_password/', App_views.change_manager_password),
    path('add_new_book/', App_views.add_new_book),
    path('alter_book/', App_views.alter_book),
    path('', App_views.login),
]

7.MyApp/model.py(添加模型)

from django.db import models
class User(models.Model):  #用户表
    account=models.CharField(max_length = 20,primary_key=True)#账号
    user_password=models.CharField(max_length = 20)#用户密码
    user_identity=models.CharField(max_length = 20)#用户身份
class Student(models.Model):  #学生信息表
    student_id=models.CharField(max_length = 20,primary_key=True)#学号 主键
    student_name=models.CharField(max_length=20)#姓名
    student_tel=models.CharField(max_length = 20)#电话
    student_major=models.CharField(max_length = 20)#院系
    student_email=models.CharField(max_length = 50)#邮箱
class Manager(models.Model):  #图书管理员信息表
    manager_id=models.CharField(max_length = 20,primary_key=True)#工号 主键
    manager_name=models.CharField(max_length=20)#姓名
    manager_tel=models.CharField(max_length = 20)#电话
    manager_email=models.CharField(max_length = 50)#邮箱
    manager_stack=models.CharField(max_length = 20)#管理书库
class Type(models.Model):#书籍类型表
    type_id= models.CharField(max_length=20,primary_key=True)  # 类型编号,主键
    type_name = models.CharField(max_length=20)  # 类型名称
class Book(models.Model):#书本信息表
    ISBN= models.CharField(max_length = 20,primary_key=True)  # 国际标准书号 主键
    book_name = models.CharField(max_length=20)  # 书名
    book_author = models.CharField(max_length=20)  # 作者
    book_publisher = models.CharField(max_length=20)  # 出版社
    book_version = models.CharField(max_length=20)  # 版本
    book_price = models.CharField(max_length=20)  # 价格
    book_number = models.IntegerField()  # 总库存数(馆藏数)
    book_rest = models.IntegerField()  # 可借数
    book_place = models.CharField(max_length=20)  # 所属书库
    book_type = models.ForeignKey(Type, on_delete=models.CASCADE)#书籍类型
class Borrow(models.Model):#借阅表
    student_id= models.CharField(max_length=20)  # 借书人学号
    student_name = models.CharField(max_length=20)  # 借书人姓名
    student_tel = models.CharField(max_length=20)  # 借书人联系方式
    book_id = models.CharField(max_length=20)  # 书籍编号
    book_name = models.CharField(max_length=20)  # 书名
    borrow_time = models.CharField(max_length=20)  # 借书时间
    rest_time = models.IntegerField()  # 剩余天数

8.MyApp/views.py视图函数

import datetime
from django.shortcuts import render
from MyApp.models import *
account=""
global_sname=""
def login(request):#登录
    return render(request, 'login.html')
def student_register(request):  # 学生注册
    name = request.POST.get("student_name")  # 获取学生输入的姓名
    id = request.POST.get("student_id")  # 获取学生输入的学号
    major = request.POST.get("student_major")  # 获取学生输入的学院
    email = request.POST.get("student_email")  # 获取学生输入的邮箱
    telephone = request.POST.get("student_telephone")
    password = request.POST.get("student_password")
    result1 = User.objects.filter(account=telephone)  # 在用户表中搜索该用户名的记录
    result2 = Student.objects.filter(student_id=id)  # 在学生表中搜索该学号的记录
    context = {}
    if len(result1) == 1:  # 判断该账户是否存在(即判断是否注册过),如果后台存在记录,则返回相应的提示语句
        context["info"] = "该账户已注册!!!"
        context["status"] = 0  #零表示注册失败
        return render(request, 'login.html', context=context)
    else:  #该账户是新用户
        if len(result2) == 1:#判断该学号是否有学生已使用
            context["info"] = "该学号已占用!!!"
            context["status"] = 4
            return render(request, 'login.html', context=context)
        else:
            User.objects.create(account=telephone, user_password=password,user_identity='学生')#用create为user表添加一条记录
            Student.objects.create(student_name=name,student_id=id,student_major=major,student_tel=telephone,student_email=email)#用create为student表添加一条记录
            context["info"] = "注册成功!"
            context["status"] = 1  #1表示注册成功
            return render(request, 'login.html', context=context)
def manager_register(request):  # 管理员注册
    name = request.POST.get("manager_name")  # 获取管理员输入的姓名
    id = request.POST.get("manager_id")  # 获取管理员输入的工号
    stack = request.POST.get("manager_stack")  # 获取管理员输入的书库
    email = request.POST.get("manager_email")  # 获取管理员输入的邮箱
    telephone = request.POST.get("manager_telephone")
    password = request.POST.get("manager_password")
    result1 = User.objects.filter(account=telephone)  # 在用户表中搜索该用户名的记录
    result2 = Manager.objects.filter(manager_id=id)  # 在管理员表中搜索该工号的使用记录
    context = {}
    if len(result1) == 1:  # 判断该账户是否存在(即判断是否注册过),如果后台存在记录,则返回相应的提示语句
        context["info"] = "该账户已注册!!!"
        context["status"] = 0  #零表示注册失败
        return render(request, 'login.html', context=context)
    else:  #该账户是新用户
        if len(result2) == 1:#判断该工号号是否有管理员已使用
            context["info"] = "该工号已占用!!!"
            context["status"] = 5
            return render(request, 'login.html', context=context)
        else:
            User.objects.create(account=telephone, user_password=password,user_identity='管理员')#用create为user表添加一条记录
            Manager.objects.create(manager_name=name, manager_id=id, manager_stack=stack, manager_tel=telephone,manager_email=email)#用create为manager表添加一条记录
            context["info"] = "注册成功!"
            context["status"] = 1  #1表示注册成功
            return render(request, 'login.html', context=context)
def login_judge(request):#登入判定
    global account ,global_sname,global_mname #定义全局变量account,存储该用户的账户,global_sname保存一下该学生的姓名,global_mname保存一下该学生的姓名
    account = request.POST.get("telephone")#获取前端输入的账户(手机号)
    user_password = request.POST.get("password")
    result1 = User.objects.filter(account=account)#在user表里检索是否存在该账户
    if len(result1) == 1:  # 判断后台是否存在该用户,有则进一步判断密码是否正确
        password = result1[0].user_password  # 获取后台的密码
        identity = result1[0].user_identity  # 获取该账户的身份信息
        if user_password == password:  # 将用户输入的密码和后台密码进行比对,如何正确,判断该账户身份
            if identity == '学生':
                result2 = Student.objects.filter(student_tel=account)
                global_sname = result2[0].student_name  # 用全局变量保存一下该学生的姓名
                context={
                    "name":result2[0].student_name,
                    "id":result2[0].student_id,
                    "major":result2[0].student_major,
                    "telephone":result2[0].student_tel,
                    "email":result2[0].student_email,
                }
                return render(request, 'student/student_information.html',context)  # 跳转到学生主页界面
            else:
                result = Manager.objects.filter(manager_tel=account)  # account为全局变量
                global_mname = result[0].manager_name  # 用全局变量保存一下该管理员的姓名
                context = {
                    "name": result[0].manager_name,
                    "id": result[0].manager_id,
                    "stack": result[0].manager_stack,
                    "telephone": result[0].manager_tel,
                    "email": result[0].manager_email,
                }
                return render(request, 'manager/manager_information.html',context)  # 跳转到管理员主页界面
        else:  # 如果不一致则返回相应提示语句
            context = {
                "info": "密码错误!!!",
                "status": 2
            }
            return render(request, 'login.html', context=context)  # 密码错误回到登入界面
    else:  # 如果不存在该用户则返回相应的提示语句
        context = {
            "info": "该账户不存在!!!",
            "status": 3
        }
        return render(request, 'login.html', context=context)  # 账户不存在则继续回到登入界面
def student_information(request):#个人信息
    if request.method == "GET":  #此部分是当每次点击侧边导航栏的“个人信息”选项时,都重新显示该用户的个人资料
        result = Student.objects.filter(student_tel=account)  #account为全局变量
        context = {
            "name": result[0].student_name,
            "id": result[0].student_id,
            "major": result[0].student_major,
            "telephone": result[0].student_tel,
            "email": result[0].student_email,
        }
        return render(request, 'student/student_information.html', context)#将该用户的个人信息再次传到前端页面
    else:  #在student_information.html页面的第44行中通过post方式的“保存”按钮跳转到此处,即完成更新数据操作(保存)
        email = request.POST.get("email")  # 获取邮箱
        Student.objects.filter(student_tel=account).update(student_email=email)#更新数据
        result = Student.objects.filter(student_tel=account)  # account为全局变量   此处再次传值到前端
        context = {
            "name": result[0].student_name,
            "id": result[0].student_id,
            "major": result[0].student_major,
            "telephone": result[0].student_tel,
            "email": result[0].student_email,
        }
        return render(request, 'student/student_information.html', context)  # 将该用户的个人信息再次传到前端页面
def search_book(request):#查找书籍
    if request.method == "GET":#此部分是当用户每次点击侧边导航栏的“查找书籍”选项时,都要显示出所有书籍资料
        books = Book.objects.all()
        types = Type.objects.all()
        return render(request, 'student/search_book.html',context={"books": books,"types":types,"name":global_sname })  # 向前端传递所有查找到的书籍信息的集合
    else:#student/search_book.html页面的第56行中通过post方式的“搜索”按钮跳转到此处,即完成搜索操作
        book_name = request.POST.get("book_name")
        type_id = request.POST.get("type_id")
        types = Type.objects.all()
        if book_name:#如果书名非空,则按书名查找
            book_result = Book.objects.filter(book_name=book_name)
            if book_result:#如果找到的结果集非空,则输出
                return render(request,'student/search_book.html',context={"books":book_result,"types":types,"name":global_sname})
            else:#若搜索的结果集为0,那么输出未找到该本书!
                book_result = Book.objects.all()
                return render(request, 'student/search_book.html',context={"books": book_result, "types": types, "name": global_sname, "status": 0})
        else:
            if type_id:#如果获取的类型输入框内容不为空,则按类型查找
                book_result = Book.objects.filter(book_type=type_id)
                if book_result:#如果找到的结果集非空,则输出
                    return render(request, 'student/search_book.html', context={"books": book_result,"types":types,"name":global_sname})
                else:#若搜索的结果集为0,那么输出未找到类型的书!
                    book_result = Book.objects.all()
                    return render(request, 'student/search_book.html',context={"books": book_result, "types": types, "name": global_sname,"status":1})
            else:#都为空,则显示空列表
                return render(request, 'student/search_book.html')
def borrow_book(request):
    book_ISBN = request.GET.get("book_ISBN")
    result = Book.objects.filter(ISBN=book_ISBN).first()
    books = Book.objects.all()
    types = Type.objects.all()
    if result.book_rest:#如果可借数不为0,则进行book_rest--
        rest = result.book_rest-1
        Book.objects.filter(ISBN=book_ISBN).update(book_rest=rest)
        now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")#获取当前借书的系统时间
        student = Student.objects.filter(student_tel=account).first()
        Borrow.objects.create(student_id=student.student_id,student_name=student.student_name,student_tel=account,book_id=book_ISBN,book_name=result.book_name,borrow_time=now_time,rest_time=60)
        return render(request, 'student/search_book.html',context={"books": books, "types": types, "name": global_sname})  # 向前端传递所有查找到的书籍信息的集合
    else:#可借数为0,则不予借出
        return render(request, 'student/search_book.html',context={"books": books, "types": types, "name": global_sname})  # 向前端传递所有查找到的书籍信息的集合
def borrow_record(request):#借书记录
    if request.method == "GET":
        records = Borrow.objects.filter(student_tel=account)#把当前用户的借阅记录搜索出来
        #计算剩余天数
        for record in records:
            borrow_t = record.borrow_time  #获取借阅时间如:2019-11-1 11:40
            print(borrow_t)
            str1 = borrow_t.split(' ')  # 先用空格分割该时间字符串,并保存到列表,str1[0]='2019-11-1' ,str1[1]='11:40'
            str2 = str1[0].split('-')  #再讲时间按'-'分割开,得到str2,str2[0]='2019',str2[1]='11',str2[2]='1'
            borrow_time = datetime.date(int(str2[0]), int(str2[1]), int(str2[2]))#利用date函数得到相对应的借阅时间
            now_time = datetime.date(datetime.datetime.now().year, datetime.datetime.now().month,
                               datetime.datetime.now().day)  # 获取当前日期
            rest_day = 60 - (now_time - borrow_time).days  #最多借阅60天
            print(rest_day)
            if rest_day>=0:
                Borrow.objects.filter(borrow_time = record.borrow_time).update(rest_time = rest_day)
            else:
                Borrow.objects.filter(borrow_time = record.borrow_time).update(rest_time = 0)
        return render(request,'student/borrow_record.html',context={"records":records,"name":global_sname})
def return_book(request):#还书操作,在borrow_record.html页面中点击还书按钮后跳转到此处
    borrow_id = request.GET.get("borrow_id")
    result1 = Borrow.objects.filter(id = borrow_id).first()
    result2 = Book.objects.filter(ISBN = result1.book_id).first()
    rest =  result2.book_rest+1 #还书后库存+1
    Book.objects.filter(ISBN = result2.ISBN).update(book_rest = rest)
    Borrow.objects.filter(id=borrow_id).delete()  # 当点击还书按钮后,删除该用户的借阅记录
    records = Borrow.objects.filter(student_tel=account)  # 把当前用户的借阅记录搜索出来
    return render(request, 'student/borrow_record.html', context={"records": records, "name": global_sname})
def change_password(request):#修改密码
    result = User.objects.filter(account=account).first()
    password = result.user_password
    if request.method == "GET": #此部分是当每次点击侧边导航栏的“修改密码”选项时,显示该界面
        return render(request,'student/change_password.html',context={"password":password,"name":global_sname})
    else:#此部分是在change_password.html页面中点击保存按钮时完成修改密码的操作
        oldPassword = request.POST.get("oldPassword")
        newPassword = request.POST.get("newPassword")
        reNewPassword = request.POST.get("reNewPassword")#以下是先判断输入的旧密码是否正确,并且两次输入的密码是否一致且都不为空
        if password == oldPassword and newPassword == reNewPassword and newPassword and reNewPassword:
            User.objects.filter(account=account).update(user_password = newPassword)#更新该用户的密码
            password = newPassword
        return render(request, 'student/change_password.html', context={"password": password, "name": global_sname})
#管理员界面
def manager_information(request):#个人信息
    if request.method == "GET":  #此部分是当每次点击侧边导航栏的“个人信息”选项时,都重新显示该管理员的个人资料
        result = Manager.objects.filter(manager_tel=account)  #account为全局变量
        context = {
            "name": result[0].manager_name,
            "id": result[0].manager_id,
            "stack": result[0].manager_stack,
            "telephone": result[0].manager_tel,
            "email": result[0].manager_email,
        }
        return render(request, 'manager/manager_information.html', context)#将该用户的个人信息再次传到前端页面
    else:  #在manager_information.html页面的第44行中通过post方式的“保存”按钮跳转到此处,即完成更新数据操作(保存)
        stack = request.POST.get("stack")  # 获取书库信息
        email = request.POST.get("email")  # 获取邮箱
        Manager.objects.filter(manager_tel=account).update(manager_email=email,manager_stack=stack)#更新数据
        result = Manager.objects.filter(manager_tel=account)  # account为全局变量   此处再次传值到前端
        context = {
            "name": result[0].manager_name,
            "id": result[0].manager_id,
            "stack": result[0].manager_stack,
            "telephone": result[0].manager_tel,
            "email": result[0].manager_email,
        }
        return render(request, 'manager/manager_information.html', context)  # 将该用户的个人信息再次传到前端页面
def manage_book(request):#管理书籍
    if request.method == "GET":  # 此部分是当用户每次点击侧边导航栏的“管理书籍”选项时,都要显示出所有书籍资料
        books = Book.objects.all()
        types = Type.objects.all()
        return render(request, 'manager/manage_book.html',context={"books": books, "types": types, "name": global_mname})  # 向前端传递所有查找到的书籍信息的集合
    else:  # 在manager/manage_bok.html页面中通过post方式的“搜索”按钮跳转到此处,即完成搜索操作
        book_name = request.POST.get("book_name")
        type_id = request.POST.get("type_id")
        types = Type.objects.all()
        if book_name:  # 如果书名非空,则按书名查找
            book_result = Book.objects.filter(book_name=book_name)
            if book_result:  # 如果找到的结果集非空,则输出
                return render(request, 'manager/manage_book.html',context={"books": book_result, "types": types, "name": global_mname})
            else:  # 若搜索的结果集为0,那么输出未找到该本书!
                book_result = Book.objects.all()
                return render(request, 'manager/manage_book.html',
                              context={"books": book_result, "types": types, "name": global_mname, "status": 0})
        else:
            if type_id:  # 如果获取的类型输入框内容不为空,则按类型查找
                book_result = Book.objects.filter(book_type=type_id)
                if book_result:  # 如果找到的结果集非空,则输出
                    return render(request, 'manager/manage_book.html',
                                  context={"books": book_result, "types": types, "name": global_mname})
                else:  # 若搜索的结果集为0,那么输出未找到类型的书!
                    book_result = Book.objects.all()
                    return render(request, 'manager/manage_book.html',
                                  context={"books": book_result, "types": types, "name": global_mname, "status": 1})
            else:  # 都为空,则显示空列表
                return render(request, 'manager/manage_book.html')
def add_book(request):#增加书籍的馆藏数量
    if request.method == "GET":
        ISBN = request.GET.get("book_ISBN1")
        result = Book.objects.filter(ISBN=ISBN).first()
        number = result.book_number+1 #让该书本的馆藏数量和可借数++
        rest = result.book_rest+1
        Book.objects.filter(ISBN=ISBN).update(book_number = number,book_rest = rest)
        books = Book.objects.all()
        types = Type.objects.all()
        return render(request, 'manager/manage_book.html',context={"books": books, "types": types, "name": global_mname})  # 向前端传递所有查找到的书籍信息的集合
def reduce_book(request):#减少书籍的馆藏数量
    if request.method == "GET":
        ISBN = request.GET.get("book_ISBN2")
        result = Book.objects.filter(ISBN=ISBN).first()
        number = result.book_number - 1  #让该书本的馆藏数量和可借数--
        rest = result.book_rest -1
        Book.objects.filter(ISBN=ISBN).update(book_number = number,book_rest = rest)
        books = Book.objects.all()
        types = Type.objects.all()
        return render(request, 'manager/manage_book.html',context={"books": books, "types": types, "name": global_mname})  # 向前端传递所有查找到的书籍信息的集合
def delete_book(request):#清空该书籍
    if request.method == "GET":
        ISBN = request.GET.get("ISBN")
        print(ISBN)
        Book.objects.filter(ISBN = ISBN).delete()#在book表里删除该条记录
        books = Book.objects.all()
        types = Type.objects.all()
        return render(request, 'manager/manage_book.html',context={"books": books, "types": types, "name": global_mname})  # 向前端传递所有查找到的书籍信息的集合
def alter_book(request):#修改书本详情
    types = Type.objects.all()
    if request.method == "GET":#此部分是当用户在manage_book.html页面中点击修改书籍是执行,目的是显示当前书本的信息
        ISBN = request.GET.get("book_ISBN3")
        result = Book.objects.filter(ISBN=ISBN).first()
        context={
            "ISBN": result.ISBN,
            "book_name": result.book_name,
            "book_author": result.book_author,
            "book_publisher": result.book_publisher,
            "book_version": result.book_version,
            "book_price": result.book_price,
            "book_number": result.book_number,
            "book_rest": result.book_rest,
            "book_place": result.book_place,
            "type_name": result.book_type.type_name,
            "name": global_sname,
            "types": types
        }
        return render(request, 'manager/alter_book.html',context)  # 向前端传递该书籍的所有信息
    else:#此部分是当用户在alter_book.html页面中点击保存按钮后重新更新用户修改后的信息
        ISBN = request.POST.get("ISBN")
        book_name = request.POST.get("book_name")
        book_author = request.POST.get("book_author")
        book_publisher = request.POST.get("book_publisher")
        book_version = request.POST.get("book_version")
        book_price = request.POST.get("book_price")
        book_number = request.POST.get("book_number")
        book_rest = request.POST.get("book_rest")
        book_place = request.POST.get("book_place")
        type_name = request.POST.get("type_name")
        if book_number.isdigit() and book_rest.isdigit():  # 判断输入的馆藏数和可借数是否为数字
            type = Type.objects.filter(type_name=type_name).first()  # 书籍类型是外键
            Book.objects.filter(ISBN=ISBN).update( book_name=book_name, book_author=book_author, book_publisher=book_publisher,
                                                   book_version = book_version,
                                                   book_price = book_price, book_number=book_number, book_rest=book_rest,
                                                   book_place = book_place, book_type=type)  # 在book表里更新刚才修改的书本信息
            context = {       #把修改后的内容显示出来
                "ISBN": ISBN,
                "book_name": book_name,
                "book_author": book_author,
                "book_publisher": book_publisher,
                "book_version": book_version,
                "book_price": book_price,
                "book_number": book_number,
                "book_rest": book_rest,
                "book_place": book_place,
                "type_name": type_name,
                "name": global_sname,
                "types": types
            }
            return render(request, 'manager/alter_book.html',context)  # 重新向前端传递该书籍的所有信息
        else:
            result = Book.objects.filter(ISBN=ISBN).first()
            context = {
                "ISBN": result.ISBN,
                "book_name": result.book_name,
                "book_author": result.book_author,
                "book_publisher": result.book_publisher,
                "book_version": result.book_version,
                "book_price": result.book_price,
                "book_number": result.book_number,
                "book_rest": result.book_rest,
                "book_place": result.book_place,
                "type_name": result.book_type.type_name,
                "name": global_sname,
                "types": types
            }
            return render(request, 'manager/alter_book.html', context)  # 向前端传递该书籍的所有信息
def change_manager_password(request):#修改管理员的密码
    result = User.objects.filter(account=account).first()
    password = result.user_password
    if request.method == "GET":#此部分是当每次点击侧边导航栏的“修改密码”选项时,显示该界面
        return render(request,'manager/change_manager_password.html',context={"password":password,"name":global_mname})
    else:#此部分是在change_manager_password.html页面中点击保存按钮时完成修改密码的操作
        oldPassword = request.POST.get("oldPassword")
        newPassword = request.POST.get("newPassword")
        reNewPassword = request.POST.get("reNewPassword")#以下是先判断输入的旧密码是否正确,并且两次输入的密码是否一致且都不为空
        if password == oldPassword and newPassword == reNewPassword and newPassword and reNewPassword:
            User.objects.filter(account=account).update(user_password = newPassword)#更新该用户的密码
            password = newPassword
        return render(request, 'manager/change_manager_password.html', context={"password": password, "name": global_mname})
def add_new_book(request):#添加新书籍
    types = Type.objects.all()
    if request.method == "GET":#此部分是当每次点击侧边导航栏的“采购书籍”选项时,显示该界面
        return render(request, 'manager/add_new_book.html', context={ "name": global_mname,"types":types})
    else:#此部分是在add_new_book.html页面中点击确认按钮后完成的添加书籍操作
        ISBN = request.POST.get("ISBN")#获取用户在前端输入框中的数据
        book_name = request.POST.get("book_name")
        book_author = request.POST.get("book_author")
        book_publisher = request.POST.get("book_publisher")
        book_version = request.POST.get("book_version")
        book_price = request.POST.get("book_price")
        book_number = request.POST.get("book_number")
        book_rest = request.POST.get("book_rest")
        book_place = request.POST.get("book_place")
        type_name = request.POST.get("type_name")
        if book_number.isdigit() and book_rest.isdigit():#判断输入的馆藏数和可借数是否为数字
            type = Type.objects.filter(type_name = type_name).first()#书籍类型是外键
            Book.objects.create(ISBN=ISBN,book_name=book_name,book_author=book_author,book_publisher=book_publisher,book_version=book_version,
                                book_price=book_price,book_number=book_number,book_rest=book_rest,book_place=book_place,book_type=type)#在book表里添加新记录
            return render(request, 'manager/add_new_book.html', context={ "name": global_mname,"types":types})
        else:
            return render(request, 'manager/add_new_book.html', context={ "name": global_mname,"types":types})

9.templates/login.html

<!DOCTYPE html>
<html>
<head>
<title>欢迎来到图书管登录界面</title>
<!-- For-Mobile-Apps-and-Meta-Tags -->
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="keywords" content="" />
  <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<!-- //For-Mobile-Apps-and-Meta-Tags -->
<link href="../static/css/style1.css" type="text/css" rel="stylesheet" media="all">
<link href="../static/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
<style>
    .p_one{
        color: #4fff24;
        position: relative;
        left: 636px;
        top: 69px;
        font-size: 23px;
        font-family: monospace;
    }
</style>
</head>
<body>
<section>
{% if status == 0 %}
    <p class="p_one"> {{ info }} </p>
{% elif status == 1 %}
    <p class="p_one"> {{ info }} </p>
{% elif status == 2 %}
    <p class="p_one"> {{ info }} </p>
{% elif status == 3 %}
    <p class="p_one"> {{ info }} </p>
{% elif status == 4 %}
    <p class="p_one"> {{ info }} </p>
{% elif status == 5 %}
    <p class="p_one"> {{ info }} </p>
{% elif status == 6 %}
    <p class="p_one"> {{info}} </p>
{% endif %}
  <div class="stage">
   <h1>欢迎来到惠经图书馆</h1>
    <div class="cbImage w3">
    <h3>学生注册</h3>
    <form action="/student_register/" method="post">
            {% csrf_token %}
      <input type="text"  name="student_name" placeholder="姓名" required>
      <input type="text"  name="student_id" placeholder="学号" required>
            <select name="student_major"required>
                <option value="信息工程学院">信息工程学院</option>
                <option value="经济管理学院">经济管理学院</option>
                <option value="艺术与设计学院">艺术与设计学院</option>
                <option value="教育与应用外语学院">教育与应用外语学院</option>
                <option value="建筑工程学院">建筑工程学院</option>
                <option value="智能制造与汽车工程学院">智能制造与汽车工程学院</option>
                <option value="体育学院">体育学院</option>
                <option value="财经学院">财经学院</option>
            </select>
            <input type="text"  name="student_email" placeholder="邮箱" required>
            <input type="text"  name="student_telephone" placeholder="电话" required>
            <input type="password"  name="student_password" placeholder="密码" required>
      <input type="submit" class="" value="注册"style="position: relative;top: 15px;left: 11px;">
    </form>
  </div>
    <div class="cbImage active signin agileits">
    <h3>登录</h3>
    <form action="/login_judge/" method="post"><!--提交到login_judge登录判定函数-->
            {% csrf_token %}
      <input type="text" name="telephone" class="name" placeholder="手机号" required>
      <input type="password" name="password" class="password" placeholder="密码" required>
      <input type="submit" value="登录"style="position: relative;top: 47px;left: 11px;">
    </form>
  </div>
    <div class="cbImage agileinfo providerAdd">
    <h3>图书管理员注册</h3>
    <form action="/manager_register/" method="post">
            {% csrf_token %}
      <input type="text"  name="manager_name" placeholder="姓名" required>
      <input type="text"  name="manager_id" placeholder="工号" required=>
            <select name="manager_stack"required>
                <option value="A区">A区</option>
                <option value="B区">B区</option>
                <option value="C区">C区</option>
                <option value="D区">D区</option>
            </select>
            <input type="text"  name="manager_email" placeholder="邮箱" required="">
            <input type="text"  name="manager_telephone" placeholder="电话" required="">
            <input type="password"  name="manager_password" placeholder="密码" required="">
      <input type="submit" value="注册"style="position: relative;top: 10px;left: 11px;">
    </form>
  </div>
    <div class="clear"></div>
  </div>
</section>
<script src="../static/js/jquery-2.1.4.min.js"></script>
<script src="../static/js/coverflow-slideshow.js"></script>
</body>
</html>

10.templates/student_information.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>图书管理系统</title>
    <link rel="stylesheet" href="../../static/css/public.css"/>
    <link rel="stylesheet" href="../../static/css/style.css"/>
</head>
<body>
<!--头部-->
    <header class="publicHeader">
        <h1>图书管理系统</h1>
        <div class="publicHeaderR">
            <p><span>下午好!</span><span style="color: #fff21b"> {{ name }}</span> , 欢迎你!</p>
            <a href="/login?">退出</a>
        </div>
    </header>
<!--时间-->
    <section class="publicTime">
        <span id="time"></span>
        <a href="#">温馨提示:为了能正常浏览,请使用google浏览器!</a>
    </section>
<!--主体内容-->
    <section class="publicMian ">
        <div class="left">
            <h2 class="leftH2"><span class="span1"></span>功能列表 <span></span></h2>
            <nav>
                <ul class="list">
                    <li><a href="/student_information?">个人信息</a></li>
                    <li><a href="/search_book?">查找书籍</a></li>
                    <li><a href="/borrow_record?">借书记录</a></li>
                    <li><a href="/change_password?">密码修改</a></li>
                    <li><a href="/login?">退出系统</a></li>
                </ul>
            </nav>
        </div>
        <div class="right">
            <div class="location">
                <strong>你现在所在的位置是:</strong>
                <span>个人信息管理页面</span>
            </div>
            <div class="providerAdd">
                <form action="/student_information/"method="post">
                    {% csrf_token %}
                    <div>
                        <label >姓名:</label>
                        <input type="text" value="{{ name }}"  readonly/><img src="../../static/img/timg.jpg"style="position: relative;
    right: 25px;top: 5px;">
                    </div>
                    <div>
                        <label >学号:</label>
                        <input type="text" value="{{ id }}"  readonly/><img src="../../static/img/timg.jpg"style="position: relative;
    right: 25px;top: 5px;">
                    </div>
                    <div>
                        <label >学院:</label>
                        <input type="text" value="{{ major }}" readonly/><img src="../../static/img/timg.jpg"style="position: relative;
    right: 25px;top: 5px;">
                    </div>
                    <div>
                        <label >电话:</label>
                        <input type="text" value="{{ telephone }}" name="telephone"readonly /><img src="../../static/img/timg.jpg"style="position: relative;
    right: 25px;top: 5px;">
                    </div>
                    <div>
                        <label >邮箱:</label>
                        <input type="text" value="{{ email }}"name="email" />
                    </div>
                    <div class="providerAddBtn">
                        <!--<a href="#">保存</a>-->
                        <input type="submit"value="保存"onclick="alert('保存成功!')">
                    </div>
                </form>
            </div>
        </div>
<script src="../../static/js/jquery.js"></script>
<script src="../../static/js/js.js"></script>
<script src="../../static/js/time.js"></script>
</body>
</html>

11.数据库迁移

python manage.py makemigrations

12.更新数据库

python manage.py migrate

13.运行项目

python manage.py runserver

因为前端网页页面比较多,所以简单写了两个,需要源码的可以私信我拿

在本次的图书管理系统开发中,我们使用了Django框架,通过MVT的设计模式,实现了图书的增删改查等基本功能。同时,我们还通过使用Bootstrap框架和jQuery库,使得系统界面更加美观、易用。本系统不仅满足了基本的图书管理需求,还具备了一些高级功能,如图书的分页显示和搜索等。在开发过程中,我们遇到了许多问题,但通过不懈的努力和团队合作,最终成功地完成了系统的开发和测试。我们相信,这个系统将会在实际应用中发挥重要的作用,为图书馆和读者提供更加便捷的服务。


相关文章
|
3月前
|
前端开发 关系型数据库 MySQL
Python基于Django框架图书管理系统,Bootstrap框架UI,后台EasyUI框架UI,有登录,实现增删改查的富文本效果
本文介绍了一个使用Python Django框架开发的图书管理系统,该系统采用Bootstrap框架进行前端UI设计,EasyUI框架用于后台UI界面,集成了富文本编辑器,并实现了登录及增删改查功能。
|
5月前
|
前端开发 JavaScript 数据库
Django项目之图书管理系统
Django项目之图书管理系统
|
Web App开发 前端开发 数据库
Python高级进阶(二)Python框架之Django写图书管理系统(LMS)
正式写项目准备前的工作   Django是一个Web框架,我们使用它就是因为它能够把前后端解耦合而且能够与数据库建立ORM,这样,一个Python开发工程师只需要干自己开发的事情就可以了,而在使用之前就我们需要给Django做文件配置和数据库配置   上一章写过的文章,有些朋友反应,对于Web框...
2065 0
|
7天前
|
设计模式 前端开发 数据库
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第27天】本文介绍了Django框架在Python Web开发中的应用,涵盖了Django与Flask等框架的比较、项目结构、模型、视图、模板和URL配置等内容,并展示了实际代码示例,帮助读者快速掌握Django全栈开发的核心技术。
79 44
|
3月前
|
机器学习/深度学习 数据采集 数据可视化
基于爬虫和机器学习的招聘数据分析与可视化系统,python django框架,前端bootstrap,机器学习有八种带有可视化大屏和后台
本文介绍了一个基于Python Django框架和Bootstrap前端技术,集成了机器学习算法和数据可视化的招聘数据分析与可视化系统,该系统通过爬虫技术获取职位信息,并使用多种机器学习模型进行薪资预测、职位匹配和趋势分析,提供了一个直观的可视化大屏和后台管理系统,以优化招聘策略并提升决策质量。
162 4
|
3月前
|
搜索推荐 前端开发 数据可视化
【优秀python web毕设案例】基于协同过滤算法的酒店推荐系统,django框架+bootstrap前端+echarts可视化,有后台有爬虫
本文介绍了一个基于Django框架、协同过滤算法、ECharts数据可视化以及Bootstrap前端技术的酒店推荐系统,该系统通过用户行为分析和推荐算法优化,提供个性化的酒店推荐和直观的数据展示,以提升用户体验。
142 1
|
8天前
|
安全 数据库 开发者
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第26天】本文详细介绍了如何在Django框架下进行全栈开发,包括环境安装与配置、创建项目和应用、定义模型类、运行数据库迁移、创建视图和URL映射、编写模板以及启动开发服务器等步骤,并通过示例代码展示了具体实现过程。
25 2
|
11天前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
22 1
|
2月前
|
机器学习/深度学习 人工智能 算法
植物病害识别系统Python+卷积神经网络算法+图像识别+人工智能项目+深度学习项目+计算机课设项目+Django网页界面
植物病害识别系统。本系统使用Python作为主要编程语言,通过收集水稻常见的四种叶片病害图片('细菌性叶枯病', '稻瘟病', '褐斑病', '稻瘟条纹病毒病')作为后面模型训练用到的数据集。然后使用TensorFlow搭建卷积神经网络算法模型,并进行多轮迭代训练,最后得到一个识别精度较高的算法模型,然后将其保存为h5格式的本地模型文件。再使用Django搭建Web网页平台操作界面,实现用户上传一张测试图片识别其名称。
114 22
植物病害识别系统Python+卷积神经网络算法+图像识别+人工智能项目+深度学习项目+计算机课设项目+Django网页界面
|
22天前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
19 4