Could not convert a value to the parameter's type
The error
The error page has this title:
Problem With the Route's Arguments
Under the title, one sentence names the route function ("Drafter could not match the information sent by the page to the parameters of your 'set_price' function."). The technical message and the "What To Try Next" list then give the detail that identifies this 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 the value came from, and it ends with 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. When the box was left empty, the message says "expects float but no value was provided" and its hint says how to allow that: give the parameter a default value, or make it optional withfloat | None. Blank values lists what each type receives for an empty field.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.