Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Discussion options

You must be logged in to vote

Make it fully dynamic seems to be impossible.
But If you just want to keep the form and the processing endpoint in sync, then something like this should help:

from typing import Annotated

from fastapi import FastAPI, Form, Request
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel


templates = Jinja2Templates(directory="templates")


class FormParams(BaseModel):
    name: str
    age: int
    other: str


app = FastAPI()


@app.get("/")
async def get_form(request: Request):
    return templates.TemplateResponse(
        request=request,
        name="form.html",
        context={"fields": list(FormParams.model_fields.keys())},
    )


@app.post("/")
async def p…

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #3543 on February 28, 2023 16:34.