Could not convert a value to the parameter's type
The error
Parameter 'price' expects float but got 'free' from the form field
'price'. Try entering a number, or change the parameter type to str.
The same form of message appears for other target types; it always
names the parameter, the expected type, the value that arrived, and
where it came from, plus a hint. (Because it is the most common
case, the int version has
its own entry.)
What it means
Form values arrive as text, and Drafter converts them to whatever type the route's parameter is annotated with. This value could not become that type, so the route did not run.
Where to look
Look at the component named as the source, and at the annotation
on the route's def line. Then think about what a visitor could
type or choose there.
Check
The expected type in the message tells you which conversion failed:
float: the text was not a number at all.bool: this conversion normally cannot fail, because checkboxes send values that convert cleanly. If you see it, a text box is usually supplying aboolparameter.date,time, ordatetime: the text was not ISO-formatted; the hint suggests forms like2024-01-31or13:30:00, which the date and time inputs produce automatically.- A dataclass: the data was missing a required field; the message names which one.
Fix
Match the annotation to what the field really provides, or the field to what the annotation really needs. There are three standard approaches:
- Loosen the annotation to
strand convert inside the route, politely handling bad input yourself. - Fix the input side: a
DateInputfor a date parameter, a number-kindTextBoxfor numeric ones. - Keep the strict annotation and accept that bad input stops with this friendly error; for many course apps, that behavior is acceptable.
Confirm
Submit the same value again and the route runs (or your polite in-page message appears, if you chose the first approach). Add a test calling the route with a typed value of the right type.
Prevent
Decide each parameter's type when you create its input component, and label inputs so visitors know what belongs in them.
Understand
Forms and input has the full conversion table.