简体   繁体   English

FastAPI Post 请求未呈现 jinja2 模板

[英]FastAPI Post request is not rendering jinja2 template

I have made up the post call and render the jinja2 template of login_form.html我已经完成了 post 调用并呈现了 login_form.html 的 jinja2 模板

main.py : main.py

from fastapi import FastAPI, Response, status, HTTPException, Depends
import hashlib
from fastapi import APIRouter, Form
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
from fastapi import Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

app = FastAPI()

templates = Jinja2Templates(directory="templates")


@app.post("/",response_class=HTMLResponse)
async def login_form(
    request: Request, uname: str = Form(...), passcode: str = Form(...)
):
    print(uname, passcode)
    return templates.TemplateResponse(
        "auth/login_form.html", {"request": request,"result": "res"}
    )

login_form.html : login_form.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Login Form</title>
    </head>
<body>
<h1>Login Form</h1>
<form method="post">
<h5>Username</h5>
<input type="text" name="uname" placeholder="username"></input>
<h5>Password</h5>
<input name="passcode" placeholder="password" type="text"></input>
<input type="submit"></input>
</form>

<p>{{result}}</p>
</body>
</html>

When I load the page on web browser I get:当我在 web 浏览器上加载页面时,我得到:

{"detail":"Method Not Allowed"}

载入网页

If you want this to work in the browser, you will most likely have to make login_form() a GET endpoint.如果您希望它在浏览器中工作,您很可能必须将login_form()设置为GET端点。

If you've seen examples using POST with FastAPI, that's probably because that was for building an API rather than a web app (maybe Starlette is what you're actually looking for).如果您已经看到使用带有 FastAPI 的POST的示例,那可能是因为那是用于构建 API 而不是 web 应用程序(也许Starlette是您真正想要的)。

Here's your solution adapted to work in the browser (with FastAPI):这是适合在浏览器中工作的解决方案(使用 FastAPI):

from fastapi import FastAPI, Query, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

app = FastAPI()

templates = Jinja2Templates(directory="templates")

@app.get("/", response_class=HTMLResponse)
async def login_form(
    request: Request,
    uname: str = Query(...),     # uname and passcode are mandatory
    passcode: str = Query(...),  # query parameters now
):
    print(uname, passcode)
    return templates.TemplateResponse(
        "auth/login_form.html", {"request": request, "result": "res"}
    )

It renders like this:它呈现如下:

在此处输入图像描述

However, that's probably not exactly what you need.但是,这可能不是您所需要的。 I would assume you want to present the form without requiring uname and passcode , in which case, you should probably remove the arguments from the endpoint.我假设您想要呈现表单而不需要unamepasscode ,在这种情况下,您可能应该从端点中删除 arguments 。

You need two routes first get template then make post @app.get("/") @app.post("/")您需要两条路线,首先获取模板然后发布 @app.get("/") @app.post("/")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM