【发布时间】:2021-05-20 09:35:50
【问题描述】:
我是 Fast-API 和 Jinja2 的新手。
我正在寻找通过 Jinja2Template html 页面呈现和过滤列表的正确方法。
我开发了这个 Fast-API:
# Imports
from fastapi import FastAPI, Request
# Environment
app = FastAPI()
templates = Jinja2Templates(directory="static")
# Routes & endpoints
@app.get("/")
async def get_records(request : Request):
my_list = [
{"a": "foo", "b":"bar"},
{"a": "foo1", "b":"bar1"},
{"a": "foo2", "b":"bar2"},
]
return templates.TemplateResponse("items.html", {"request" : request, "my_list" : mylist})
# Main thread
if __name__ == "__main__":
uvicorn.run(app, host='0.0.0.0', port=42)
我也开始编写我的 ./static/items.html
{% extends "base.html" %}
{% block head %}
{{ super() }}
{% endblock %}
{% block page_content %}
<div>
Hello team
</div>
# Here is where I want my list mylist to be render with the possibility filter
{% endblock %}
正如我所说,我对 Fast-API 还很陌生,但我没有在 https://fastapi.tiangolo.com/advanced/templates/ 上找到管理它的方法。
你能帮我找到方法吗?我很确定我必须编写一个 JS 脚本。
【问题讨论】: