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

佛山网站建设原创网站底部版权信息

佛山网站建设原创,网站底部版权信息,做网站注册商标,五合一自助建站网站代码结构 . #根目录 └── todo #module名称├── static # 静态文件│ ├── logo.png # 测试用logo│ └── style.css # 代码样式├── templates│ └── todo.html# 前端页面├── main.py # 系统入口及API接口└── models.py # 实体映射前端样式 #todo_…

代码结构

. #根目录
└── todo #module名称├── static # 静态文件│   ├── logo.png # 测试用logo│   └── style.css # 代码样式├── templates│   └── todo.html# 前端页面├── main.py # 系统入口及API接口└── models.py # 实体映射

前端样式

#todo_list{border :solid 1px #0b2e13;border-collapse:collapse;
}#todo_list #header{background-color:#1e7e34;color:#white;
}#todo_list #header th{padding: 5px 10px;border:solid 1px #0b2e13;
}#todo_list #th{padding: 5px 10px;border:solid 1px #0b2e13;
}

Jinja2的前端html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Todo List</title><link rel="stylesheet" href="/static/style.css">
</head>
<body>
<h1>todo list</h1>
<form action="/addTodo" method="post"><div><label for="name">待办名称:</label><input type="text" name="name" required></div><div><label for="priority">优先级:</label><select name="priority" required><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></select></div><button type="submit">添加</button>
</form>
<table id = "todo_list"><thead><tr id="header"><th>id</th><th>名称</th><th>是否完成</th><th>优先级</th><th>创建日期</th></tr></thead><tbody><!-- 解析后端返回的参数并渲染表格 -->{% for todo in todos %}<tr><td>{{ todo.id }}</td><td>{{ todo.name }}</td><td>{{ todo.is_completed }}</td><td>{{ todo.priority }}</td><td>{{ todo.created_at }}</td><td><!--如果未完成,显示标记为完成按钮,并提供post方法,标记为已完成-->{% if not todo.is_completed %}<form action="/todo/{{ todo.id }}/complete" method="post"><button type="submit">标记为完成</button></form>{% else %}<a href="/completeTodo/{{ todo.id }}">已完成</a>{% endif %}<!--删除操作--><form action="/todo/{{ todo.id }}/delete" style="display:inline" method="post"><button type="submit">删除</button></form></td></tr>{% endfor %}</tbody>
</table>
</body>
</html>

main函数

from datetime import datetimeimport  uvicorn
from fastapi import FastAPI, Request, Form
from starlette.responses import HTMLResponse,RedirectResponse
from tortoise.contrib.fastapi import register_tortoise
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from tortoise.exceptions import DoesNotExistfrom todo.models import Todoapp = FastAPI()
# 静态文件目录
app.mount("/static", StaticFiles(directory="static"), name="static")# html 页面目录
templates = Jinja2Templates(directory="templates")
try:register_tortoise(app,db_url="mysql://root:[PWD]@[IP]:3306/tortoise_fast_api",  # 替换为你的数据库名称modules={"models": ["todo.models"]},  # 指定包含的 models 模块generate_schemas=False,  # 不自动生成表结构add_exception_handlers=True  # 添加异常处理程序)print("Tortoise ORM registered successfully.")
except Exception as e:print(f"Failed to register Tortoise ORM: {e}")# 返回类定义为HTMLResponse,返回html页面
@app.get("/todos",response_class=HTMLResponse)
async def get_todo_list(request:Request):"""使用 await Todo.all().order_by("-created_at") 获取所有待办事项并按创建时间降序排序。使用 templates.TemplateResponse 渲染 todo.html 模板,并传递请求对象和待办事项列表。"""todos:list[Todo] = await Todo.all().order_by("-created_at")return templates.TemplateResponse("todo.html",{"request":request,"todos":todos})@app.post("/addTodo",response_class=HTMLResponse)
async def add_todo(request:Request,name:str =Form(...),priority:str =Form(...)):await Todo.create(name=name,priority=priority,created_at = datetime.now())return RedirectResponse("/todos",status_code=303)@app.post("/todo/{id}/complete", response_class=RedirectResponse)
async def complete_todo(id: int):todo = await Todo.get(id=id)if not todo:return RedirectResponse("/todos", status_code=303)todo.is_completed = Trueawait todo.save()return RedirectResponse("/todos", status_code=303)@app.post("/todo/{id}/delete", response_class=RedirectResponse)
async def delete_todo(id: int):try:todo = await Todo.get(id=id)await todo.delete()except DoesNotExist:passreturn RedirectResponse("/todos", status_code=303)
if __name__ == "__main__":uvicorn.run(app,port=8888)

models 定义实体

from tortoise import fieldsfrom tortoise import Modelclass Todo(Model):id = fields.IntField(pk=True,description="待办ID")name = fields.CharField(max_length=255,description="待办名称")is_completed = fields.BooleanField(default=False,description="是否完成")priority = fields.CharField(max_length=10,description="优先级")created_at = fields.DatetimeField(description="创建时间")class Meta:table = "todo"table_description = "待办表"
http://www.dtcms.com/a/410731.html

相关文章:

  • Unity / C# 开发常见问题总结(闭包、协程、事件、GC 等易踩坑)
  • C# 集合框架完全指南:从IEnumerable到ObservableCollection的深度解析
  • 用户研究:用户研究和数据分析的根本联系与区别
  • 网站关键词优化培训jeecg 3.7 网站开发
  • 右键菜单增强工具,自定义系统功能
  • 图像分类入门:从数据到模型的深度学习核心知识解析
  • 攻防世界-Web-PHP2
  • Windows系统Web UI自动化测试学习系列3--浏览器驱动下载使用
  • 00-为什么要系统学习正则表达式?
  • 湖北网站建设检修金融股票类app网站开发
  • C++ 序列容器深度解析:vector、deque 与 list
  • 提供企业网站建设上海公司注册一网通办
  • 高效的技术支持提升用户体验
  • 满山红网站建设做家装的网站有什么
  • 建设部网站社保联网小程序注册平台
  • Mysql中GROUP_CONCAT分组聚合函数的使用以及示例
  • 2025无人机林业行业场景解决方案
  • 化肥网站模板青岛建设集团 招聘信息网站
  • 【在Ubuntu 24.04.2 LTS上安装Qt 6.9.2】
  • 家居企业网站建设渠道百度如何推广广告
  • 《MLB美职棒》运动员体质特征·棒球1号位
  • AI 应用和工业软件
  • 网站备案空壳网站制作找
  • 洛谷 P3388:【模板】割点(割顶)← Tarjan 算法
  • DeepSeek“问道”-第二章:问算法 —— 阴与阳如何在我内部舞蹈?
  • 重学JS-009 --- JavaScript算法与数据结构(九)Javascript 方法
  • Python项目中ModuleNotFoundError与FileNotFoundError的深度解决指南(附实战案例)
  • LeetCode:61.分割回文串
  • 坑: console.log,对象引用机制
  • 网站模板找超速云建站学校网站建设是什么意思