How to use Dynamic Form Data in FastAPI application function Python? #8745
-
First check
ExampleHere's a self-contained, minimal, reproducible, example with my use case: from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/do_something")
def do_something(data:str = Form(...)): # Here this data: str=Form(...)should be dynamic.....taking data from HTML form
print(data)
return ""DescriptionNormally If we have form data like: We can use something like this in FastAPI application But when the HTML changes according to inputs like: So then what do we do for taking all these inputs as parameters for the function. Also the /docs of the FastAPI application should contain these things as text fields. Something like this: Please Help Fast... Thanks Also the known solution is something around using request and request.form() But in this approach nothing shows up on /docs endpoint. I want the data to show up on the swagger docs also Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
|
Could you maybe instead receive a List of Forms and then parse the data from there? It feels like your asking for the route in fastapi to dynamically react to what it is given as part of the jinja2 for loop which doesn't seem possible from what I can see. |
Beta Was this translation helpful? Give feedback.
-
Could you please give a snippet on accepting a list of forms. Thank you |
Beta Was this translation helpful? Give feedback.
-
|
When I think about this more its not realistic to expect a 'List' of Forms. Rather it would be important for the forms your building to extract what is constant and what is dynamic and have the route work with the data as it is updated. Like if someone wanted a list of aliases along with a username that could be updated @app.post("/do_something")
def do_something(username: str = Form(...), aliases: List[str] = Form(...)):If your use case is literally having an entire Form be dynamic you may have to parse the request directly like your potential solution shows. The idea of Forms from what I understand is its not expected the entire data to be parsed can not be known before doing so. |
Beta Was this translation helpful? Give feedback.
-
So then I cannot parse the form dynamically.......So can I close this issue/question then??......Or you could go ahead and close it |
Beta Was this translation helpful? Give feedback.
-
|
I'm just a fellow user who is answering questions because I'm waiting for my own to be answered. Whether to close the issue is up to you. If you feel your question has been answered, then close the issue, otherwise you can wait to see if someone else gives a different answer. |
Beta Was this translation helpful? Give feedback.
-
|
Make it fully dynamic seems to be impossible. 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 post_form(form: Annotated[FormParams, Form()]):
return formTemplate ( <form action="/do_something" method="post">
{%for i in fields%}
<label>{{i}}</label>
<input type="text" name="{{i}}">
{%endfor%}
<input type="submit" value="Submit">
</form>Whenever you update
Hope it's not too late :) |
Beta Was this translation helpful? Give feedback.

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: