当前位置: 首页 > news >正文

FastAPI系列(19):ORM响应页面数据

 

本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/19503695

项目目录下创建目录templates

index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><!-- 最新版本的 Bootstrap 核心 CSS 文件 --><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<h1>学生信息</h1><div class="row"><div class="col-md-8 col-md-offset-2">
<!--        <p>-->
<!--            <button class="btn primary"><a href="">添加学生</a></button>-->
<!--        </p>--><table class="table table-bordered table-striped table-hover"><thead><tr><th>学生学号</th><th>学生姓名</th><th>学生班级</th></tr></thead><tbody>{% for student in students %}<tr><td>{{ student.sno }}</td><td>{{ student.name }}</td><td>{{ student.clas_id }}</td></tr>{% endfor %}</tbody></table></div>
</div></body>
</html>

  

student.py

from fastapi import APIRouter
from fastapi import Request
from fastapi.templating import Jinja2Templatesfrom templates import templates_dir
from test_orm.models import Studentstudent_api = APIRouter()@student_api.get("/index.html")
async def getAllStudent(request: Request):templates = Jinja2Templates(directory=templates_dir)# templates = Jinja2Templates("../templates")students = await Student.all()return templates.TemplateResponse("index.html", {"request": request,"students": students})

 

关于templates的目录,可以相对路径,也可以绝对路径

image

 

import os
from pathlib import Pathtemplates_dir = os.path.dirname(os.path.abspath(__file__))
print(templates_dir)# 上面和下面的代码效果一样
templates_dir = Path(__file__).parent
print(templates_dir)

  

main.py

import uvicorn
from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoisefrom test_orm.api.student import student_api
from test_orm.settings import TORTOISE_ORMapp = FastAPI()app.include_router(student_api, prefix="/student", tags=["学生接口"])# register_tortoise是注册函数,fastapi一旦运行,register_tortoise已经执行,通过传递进去的app对象,监听服务启动和终止事件
register_tortoise(app=app,config=TORTOISE_ORM,
)if __name__ == '__main__':uvicorn.run('main:app', host='127.0.0.1', port=8001, reload=True, workers=1)

  

请求结果

image

 

http://www.jsqmd.com/news/338975/

相关文章:

  • 西湖大学牵头、施一公任共同主编的Vita首期,作者为颜宁团队
  • RPA赋能银行客服中心:智能化转型路径与实践应用
  • 26.2.3
  • 2025 ACM Fellow出炉!朱军、陈宝权、贾佳亚、熊辉等19人当选,清华校友霸榜
  • RPA赋能客户服务:破解效率瓶颈,实现智能降本增效
  • Kimi K2.5登顶开源第一!15T数据训练秘籍公开,杨植麟剧透K3
  • 2026 年,鸿蒙是普通开发者最后一次上车机会?
  • 为什么说队列是万能药?
  • 【AI大模型驱动】多模态知识图谱增强型搜索交互与答案生成架构解析
  • LangChain开源框架应对长任务挑战,Deep Agents如何解决上下文管理难题
  • IDEA 2025.3 骚操作,有点跟不上了!
  • PyCharm中安装backbone失败,报错:ERROR: Could not find a version that satisfies the requirement backb...如何解决?
  • 今年后端这收入是认真的吗?
  • 2026.2.3 做题记录
  • 微信红包,腾讯元宝学不会
  • Ai 算法资源合集
  • 【问题解决】OSError: Can‘t load tokenizer for ‘xxx/xxx-model‘
  • 雷军辟谣小米二手车“崩盘”:SU7保值率第一,超特斯拉保时捷;千问App宣布投入30亿元启动春节活动;SpaceX官宣与xAI合并 | 极客头条
  • Go 微服务分布式事务 TCC 模式实战全指南
  • 企业级 AI 数据分析实践指南:Spring AI Alibaba DataAgent 全流程落地
  • CRM系统深度横评:从数据录入到管理可视化,谁真正解决了销售团队的核心痛点?
  • Ubuntu vulkan不识别NVIDIA,如何解决?
  • 专家点评Nature | 邵振华/王晓辉/刘剑峰/杨胜勇联合揭示致幻剂诱导血清素受体5-HT2AR的非经典信号通路
  • 2026CRM选型手册:7 大品牌全流程能力深度解析与对比
  • 保姆级教程|用Snakemake一键跑通RNA-seq数据分析流程
  • sklearn中的学习曲线使用时机:用sklearn来观察模型表现时,应该是在模型训练前对未训练的模型使用,还是对训练完的模型使用??
  • Nature出版集团对学术图表的要求,非常详细的解析各个要点,对其他期刊的投稿也很有参考价值
  • Science丨雷晓光团队取得生物催化领域突破
  • MATLAB R2023a 的“优化工具箱(Optimization Toolbox)”里,为什么在图形界面(GUI)里找不到“模拟退火
  • Microbiome | 中国海洋大学王高歌团队揭示海带幼苗白化病致病生物组与宿主之间的复杂相互作用