小编Mip*_*ips的帖子

如何使用 url_for() 将路径和查询数据传递到使用 FastAPI 和 Jinja2 的路由?

我想从 Jinja2 模板中调用 FastAPI 路由,并将路径查询数据(参数)传递给该路由。我在 Jinja2 模板中尝试过如下所示:

{{ url_for('function1', uustr=data.uustr, interval=1) }}
Run Code Online (Sandbox Code Playgroud)

这是我想要调用的 FastAPI 路由(为了演示目的,语法已被简化):

@app.get("/updates/data/{uustr}",response_class=HTMLResponse)
async def function1(request: Request, uustr:str, interval:int):

  return"""
<html>
    <head>
        <title>{{ uustr }}</title>
    </head>
    <body>
        <h1>{{ interval }}</h1>
    </body>
</html>
"""
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

raise ValueError('context must include a "request" key') 
ValueError: context must include a "request" key
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

python jinja2 fastapi

5
推荐指数
2
解决办法
5265
查看次数

标签 统计

fastapi ×1

jinja2 ×1

python ×1