上一页 1 ··· 11 12 13 14 15 16 17 18 19 ··· 214 下一页
摘要: 一,代码: 用户模型: # app/models/users.py from typing import Optional from sqlalchemy.ext.asyncio import AsyncSession from app.models.assets import Asset from 阅读全文
posted @ 2026-07-21 13:48 刘宏缔的架构森林 阅读(7) 评论(0) 推荐(0)
摘要: 一,功能说明: 用户表users,用户头像的地址放在assets表中, 需要查询得到: 在异步 SQLAlchemy (AsyncSession) 环境下,由于 Python 的 @property 只能是同步方法,我们绝对不能在 @property 内部使用 await 去执行数据库查询。在异步架 阅读全文
posted @ 2026-07-21 12:52 刘宏缔的架构森林 阅读(7) 评论(0) 推荐(0)
摘要: 一,现象: 在 FastAPI 的父子应用(Sub-apps)架构中,静态文件的挂载位置、路由匹配机制以及模板中的 url_for 调用方式,都会直接导致静态文件出现“有时生效、有时不生效”或者彻底失效的现象。 例子: 我的项目是一个父应用,有两个子应用admin和api,static的配置是在子应 阅读全文
posted @ 2026-07-20 16:14 刘宏缔的架构森林 阅读(7) 评论(0) 推荐(0)
摘要: 一,代码 middleware的代码: # app/admin/middleware/auth_middleware.py from urllib.parse import quote_plus from fastapi import FastAPI, Request, Depends from s 阅读全文
posted @ 2026-07-19 22:13 刘宏缔的架构森林 阅读(21) 评论(0) 推荐(0)
摘要: 一,代码 在middleware中获取用户信息: # 中间件:全自动解析登录态 # 1. 定义中间件类,必须继承 BaseHTTPMiddleware class CustomAuthMiddleware(BaseHTTPMiddleware): async def dispatch(self, r 阅读全文
posted @ 2026-07-19 17:25 刘宏缔的架构森林 阅读(13) 评论(0) 推荐(0)
摘要: 一,fastapi应用jinja2模板,使用datetime_format这个过滤器时会报错 报错信息: jinja2.exceptions.TemplateAssertionError: No filter named 'datetime_format'. 二,报错原因 FastAPI 不能直接使 阅读全文
posted @ 2026-07-19 12:47 刘宏缔的架构森林 阅读(8) 评论(0) 推荐(0)
摘要: 一,安装用到的库: pip install itsdangerous 二,代码 python代码: main.py中引入SessionMiddleware # 创建独立隔离的管理后台子应用 admin_app = FastAPI( title="管理后台系统", dependencies=[Depe 阅读全文
posted @ 2026-07-18 23:55 刘宏缔的架构森林 阅读(8) 评论(0) 推荐(0)
摘要: 一,报错信息: ModuleNotFoundError: No module named 'itsdangerous' 二,原因 FastAPI 的 SessionMiddleware 底层依赖的是 Starlette 框架的会话模块,而 Starlette 的 Session 又是基于 itsda 阅读全文
posted @ 2026-07-18 23:33 刘宏缔的架构森林 阅读(17) 评论(0) 推荐(0)
摘要: 一,要注意的环节: 要用bcrypt验证登录密码, 登录成功后要保存登录信息,要写cookie 能从cookie读取用户信息 要处理未登录拦截:如果访问需要登录才能打开的页面,则跳转到登录页面,登录成功后,还要返回之前访问的页面 二,代码 python代码: account.py # 验证密码是否正 阅读全文
posted @ 2026-07-18 22:54 刘宏缔的架构森林 阅读(8) 评论(0) 推荐(0)
摘要: 一,要注意的环节: 用户的密码保存时不能用明文,要用bcrypt加密保存 有写库操作,要做try except,发生异常时要做写日志, 有多个参数,要用wtforms做参数的校验 二,代码 python代码: # 得到bcrypt加密后的密码 def get_password_hash(passwo 阅读全文
posted @ 2026-07-18 22:39 刘宏缔的架构森林 阅读(11) 评论(0) 推荐(0)
上一页 1 ··· 11 12 13 14 15 16 17 18 19 ··· 214 下一页