使用python的 FastApi框架开发图书管理系统-前后端分离项目分享

今天给大家分享一个 我最近使用python 框架 fastapi 写的一个web项目 ,叫图书管理系统。项目主要是来巩固 python的编程技术。使用的是前端后 分离开发。
主要实现的功能:
1、用户管理:可以新增、编辑、删除用户信息。
2、图书管理:添加、修改、删除图书,并能够查看图书列表。
3、借阅管理:记录图书借阅、归还情况。
4、数据展示:通过前端页面展示系统中的图书、用户信息,提供了简洁、易用的界面。
使用技术
FastAPI:后端使用FastAPI框架
MySQL 5.7:作为关系型数据库管理系统,MySQL用于存储用户和图书信息,并支持CRUD操作。
Vue 2:前端使用Vue2框架,配合Element UI组件库,提供响应式页面和现代化用户界面。
Element UI:帮助实现简洁且功能丰富的UI设计,极大提高了前端开发效率。
先给大家看一下 页面效果
首页:在这里插入图片描述
代码结构:
在这里插入图片描述
菜单个别页面截图:
在这里插入图片描述
在这里插入图片描述
项目零星代码:

<template><div class="home-container"><el-container><el-aside width="200px"><el-menu:default-active="$route.path"class="el-menu-vertical"background-color="#304156"text-color="#bfcbd9"active-text-color="#409EFF"router><el-menu-item index="/"><i class="el-icon-s-home"></i><span slot="title">首页</span></el-menu-item><el-submenu index="1"><template slot="title"><i class="el-icon-reading"></i><span>图书管理</span></template><el-menu-item index="/category/list"><i class="el-icon-collection"></i><span slot="title">图书分类</span></el-menu-item><el-menu-item index="/book/list"><i class="el-icon-notebook-2"></i><span slot="title">图书列表</span></el-menu-item></el-submenu><el-submenu index="2"><template slot="title"><i class="el-icon-document"></i><span>借用归还</span></template><el-menu-item index="/borrow/list"><i class="el-icon-document-copy"></i><span slot="title">借用列表</span></el-menu-item><el-menu-item index="/return/list"><i class="el-icon-document-checked"></i><span slot="title">归还列表</span></el-menu-item></el-submenu><el-submenu index="3"><template slot="title"><i class="el-icon-user-solid"></i><span>会员管理</span></template><el-menu-item index="/member/list"><i class="el-icon-user"></i><span slot="title">会员列表</span></el-menu-item><el-menu-item index="/recharge/list"><i class="el-icon-money"></i><span slot="title">充值记录</span></el-menu-item></el-submenu><el-submenu index="4"><template slot="title"><i class="el-icon-user"></i><span>系统管理</span></template><el-menu-item index="/admin/list"><i class="el-icon-user"></i><span slot="title">管理员列表</span></el-menu-item></el-submenu></el-menu></el-aside><el-container><el-header><div class="header-right"><el-dropdown trigger="click" @command="handleCommand"><span class="el-dropdown-link">{{ userInfo ? userInfo.username : 'admin' }}<i class="el-icon-arrow-down el-icon--right"></i></span><el-dropdown-menu slot="dropdown"><el-dropdown-item command="logout">退出登录</el-dropdown-item></el-dropdown-menu></el-dropdown></div></el-header><el-main><router-view /></el-main></el-container></el-container></div>
</template><script>
import { mapState } from 'vuex'export default {name: 'Home',computed: {...mapState('user', ['userInfo'])},methods: {handleCommand(command) {if (command === 'logout') {this.$store.dispatch('user/resetToken').then(() => {this.$router.push('/login')})}}}
}
</script><style lang="scss" scoped>
.home-container {height: 100vh;.el-container {height: 100%;}.el-aside {background-color: #304156;.el-menu {border: none;}}.el-header {background-color: #fff;box-shadow: 0 1px 4px rgba(0,21,41,.08);display: flex;justify-content: flex-end;align-items: center;.header-right {.el-dropdown-link {cursor: pointer;color: #409EFF;}}}.el-main {background-color: #f0f2f5;padding: 0;}
}
</style>

再分享一段pyhon 后端代码:

from datetime import timedelta
from typing import Optional
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from core.config import settings
from core.security import create_access_token
from core.response import ResponseModel
from core.exceptions import CustomException
from core.deps import get_current_admin
from db.database import get_db
from models.admin import Admin
from schemas.admin import AdminLogin, Token, AdminResponse, AdminCreate, AdminUpdate, AdminListResponse
import hashlibrouter = APIRouter()@router.post("/login")
async def login(login_data: AdminLogin, db: Session = Depends(get_db)):# 查询管理员admin = db.query(Admin).filter(Admin.username == login_data.username).first()if not admin:raise CustomException(msg="用户名或密码错误")# MD5加密密码进行比对md5_password = hashlib.md5(login_data.password.encode()).hexdigest()if admin.password != md5_password:raise CustomException(msg="用户名或密码错误")# 生成访问令牌access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)access_token = create_access_token(data={"sub": str(admin.id)},expires_delta=access_token_expires)token = Token(access_token=access_token,token_type="bearer")return ResponseModel.success(data=token.model_dump(),msg="登录成功")@router.get("/info")
async def get_admin_info(admin: Admin = Depends(get_current_admin)):return ResponseModel.success(data=AdminResponse.model_validate(admin),msg="获取成功")@router.get("/list")
async def get_admin_list(db: Session = Depends(get_db),current_admin: Admin = Depends(get_current_admin),page: int = Query(1, ge=1),page_size: int = Query(10, ge=1, le=100),username: Optional[str] = None
):query = db.query(Admin)if username:query = query.filter(Admin.username.like(f"%{username}%"))total = query.count()items = [AdminResponse.model_validate(item) for item in query.offset((page - 1) * page_size).limit(page_size).all()]response = AdminListResponse(total=total, items=items)return ResponseModel.success(data=response.model_dump())@router.get("/detail/{admin_id}")
async def get_admin_detail(admin_id: int,db: Session = Depends(get_db),current_admin: Admin = Depends(get_current_admin)
):admin = db.query(Admin).filter(Admin.id == admin_id).first()if not admin:raise CustomException(msg="管理员不存在")return ResponseModel.success(data=AdminResponse.model_validate(admin))@router.post("/create")
async def create_admin(admin_in: AdminCreate,db: Session = Depends(get_db),current_admin: Admin = Depends(get_current_admin)
):# 检查用户名是否已存在if db.query(Admin).filter(Admin.username == admin_in.username).first():raise CustomException(msg="用户名已存在")# MD5加密密码md5_password = hashlib.md5(admin_in.password.encode()).hexdigest()admin = Admin(username=admin_in.username,password=md5_password,nickname=admin_in.nickname,is_super=admin_in.is_super,created_by=current_admin.username)db.add(admin)db.commit()db.refresh(admin)return ResponseModel.success(data=AdminResponse.model_validate(admin))@router.put("/update/{admin_id}")
async def update_admin(admin_id: int,admin_in: AdminUpdate,db: Session = Depends(get_db),current_admin: Admin = Depends(get_current_admin)
):admin = db.query(Admin).filter(Admin.id == admin_id).first()if not admin:raise CustomException(msg="管理员不存在")update_data = admin_in.model_dump()admin.nickname = update_data['nickname']db.commit()db.refresh(admin)return ResponseModel.success(data=AdminResponse.model_validate(admin))@router.delete("/delete/{admin_id}")
async def delete_admin(admin_id: int,db: Session = Depends(get_db),current_admin: Admin = Depends(get_current_admin)
):admin = db.query(Admin).filter(Admin.id == admin_id).first()if not admin:raise CustomException(msg="管理员不存在")# 不允许删除超级管理员if admin.is_super:raise CustomException(msg="不能删除超级管理员")db.delete(admin)db.commit()return ResponseModel.success()

项目虽然功能不是很复杂,但是对于刚开始学习编程的小伙伴,有可能也是有难度的,不过如果自己能尝试着 敲一些项目,会对自己的学习到的编程知识有一个更深的体会。对此项目有兴趣的小伙伴可以去看看学习一下。【非开源项目】
https://wwwoop.com/home/Index/projectInfo?goodsId=98&typeParam=1&subKey=-1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/557.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Redis-哨兵机制Sentinel

redis的主从复制模式下,一旦主节点出现了故障无法提供服务了,需要人工进行主从切换,同时大量的客户端需要被通知切换到新的主节点上,对于有了一定规模的应用来说,这种方案的延迟是无法接受的,于是redis2.8提供了Redis-Sentinel(哨兵)来解决这个问题. 目录 1.啥是哨兵节点: 2.r…

Java 阻塞队列:7种类型全解析

在 Java 中&#xff0c;阻塞队列&#xff08;BlockingQueue&#xff09; 是一种线程安全的队列结构&#xff0c;用于实现生产者-消费者模式。它的核心特性是在队列为空时阻塞消费者线程&#xff0c;在队列满时阻塞生产者线程&#xff0c;从而自动协调线程之间的协作。阻塞队列的…

word中的单位详解

Word中的单位转换全面指南 一、Word中支持的单位类型及转换关系 1. 常用单位类型 磅&#xff08;pt&#xff09;&#xff1a;国际通用排版单位&#xff0c;1磅 ≈ 0.03527厘米&#xff0c;1厘米 ≈ 28.35磅。厘米&#xff08;cm&#xff09;&#xff1a;公制单位&#xff0c;1厘…

stm32--SPI原理应用W25Q64(二)

目录 一 概述 二 W25Q64的介绍 简介 硬件样子 主要特性 常用 SPI 指令 三 代码部分 前言 SPI.c 新加代码第一部分&#xff1a; 新加代码第二部分&#xff1a; 新加代码第三部分&#xff1a; 新加代码第四部分&#xff1a; 新加代码第五部分&#xff08;读&#xf…

关于 c、c#、c++ 三者区别

1. 起源与定位 语言起源时间开发者定位/特点C1972年Dennis Ritchie面向过程的编程语言&#xff0c;强调底层控制与高效性能C1983年Bjarne Stroustrup在 C 的基础上加入 面向对象编程&#xff08;OOP&#xff09;C#2000年微软&#xff08;Microsoft&#xff09;类似 Java&#…

7.7晚自习作业

实操作业02&#xff1a;Spark核心开发 作业说明 请严格按照步骤操作&#xff0c;并将最终结果文件&#xff08;命名为&#xff1a;sparkcore_result.txt&#xff09;于20点前上传。结果文件需包含每一步的关键命令执行结果文本输出。 一、数据读取与转换操作 上传账户数据$…

剑指offer第2版:动态规划+记忆化搜索

前三题是同一种模型&#xff0c;所以我分别用递推、记忆化、动归来做 一、p74-JZ10 斐波那契数列 斐波那契数列_牛客题霸_牛客网 class Solution { public:int Fibonacci(int n) {// write code hereif(n1||n2) return 1;int a1,b1,c1;while(n>2){cab;ab;bc;--n;}return c…

爬虫的笔记整理

网络爬虫首先要认识http和https协议 在浏览器中发送一个http请求&#xff1a; 1.输入一个URL地址之后&#xff0c;向http服务器发送请求&#xff0c;主要分为GET和POST两种方法 2.输入URL之后&#xff0c;发送一个request请求&#xff0c;这时候服务器把response文件对象发送…

Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)

Visual Studio 安装&#xff08;保姆教程 - 更新至2025.07&#xff09; 前言安装须知安装过程1. 下载安装包2. 安装3. 注册4. 创建桌面快捷方式 前言 本教程针对 非计算机相关专业的小白用户 &#xff0c;手把手教你如何基于 win11 操作系统 安装 Visual Studio 2022。安装搭载…

【Linux网络编程】Socket - UDP

目录 V1版本 - Echo Server 初始化服务器 启动服务器 客户端 本地测试 网络测试 优化代码 V2版本 - Dict Server 服务器的调整 字典 网络模块与业务模块耦合 V3版本 - 简单聊天室 简单聊天室概述 消息转发模块 数据接收模块 重定向观察 补充细节 在这一篇文章…

【STM32】通用定时器PWM

STM32 通用定时器 PWM 输出完全解析&#xff08;以 TIM3_CH1 为例&#xff09; PWM 输出基本原理 PWM&#xff08;Pulse Width Modulation&#xff09;即脉冲宽度调制&#xff0c;是由定时器通过比较 CNT 与 CCR 寄存器实现的。 信号产生原理&#xff1a; ARR 决定周期&#…

重塑数学边界:人工智能如何引领数学研究的新纪元

目录 一、人工智能如何重新定义数学研究的边界 &#xff08;一&#xff09;数学与AI的关系&#xff1a;从基础理论到创新思维的回馈 &#xff08;二&#xff09;AI的创造力&#xff1a;突破传统推理的局限 &#xff08;三&#xff09;AI对数学研究的潜在贡献&#xff1a;创…