I’m trying to capture the selected option from an HTML dropdown and send it to my FastAPI backend. When I submit the form, I get an error saying a field is missing. Can someone help me figure out what’s wrong?
Here’s my FastAPI code:
from fastapi import FastAPI, Request, Form
from fastapi.templating import Jinja2Templates
api = FastAPI()
templates = Jinja2Templates(directory="templates/")
@api.get('/')
def home():
return 'welcome'
@api.get("/dropdown")
def show_form(request: Request):
message = "Pick a color"
return templates.TemplateResponse('dropdown.html', context={'request': request, 'message': message})
@api.post("/dropdown")
def handle_form(request: Request, message = Form(...)):
return templates.TemplateResponse('dropdown.html', context={'request': request, 'message': message})
And here’s my HTML template:
<!DOCTYPE html>
<html>
<head>
<title>Color Picker</title>
</head>
<body>
<form method="post">
<select name="colors" id="colors">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
</select>
<button type="submit">Choose</button>
</form>
<p>Selected: {{ message }}</p>
</body>
</html>
I keep getting this error when I try to submit:
{"detail":[{"loc":["body","message"],"msg":"field required","type":"value_error.missing"}]}
I want to select a color from the dropdown, click the button, and see the chosen color displayed on the page.