Could not convert text to an int
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_age' function."). The technical message and the "What To Try Next" list then give the detail that identifies this error:
Parameter 'age' expects int but got 'twenty' from the form field
'age'. Try entering a number, or change the parameter type to str.
or, when the text is a decimal number:
Parameter 'price' expects int but got '2.5' from the form field
'price'. This looks like a decimal number; use float instead of int,
or enter a whole number.
or, when the box was left empty:
Parameter 'age' expects int but no value was provided by the form field
'age'. Enter a number. To let this be left blank, give the parameter a
default value or make it optional (int | None).
What it means
A form field's contents are always text; your route asked for an
int, so Drafter tried to convert the text into a whole number and
could not. The route did not run.
Where to look
The message names the parameter and the form field. Find that
component on the page and the parameter on the route's def line;
then consider what a visitor might type into that field.
Check
The hint at the end of the message distinguishes the causes:
- "Try entering a number": the text was not numeric at all
(
"twenty","3 dogs"). - "use float instead of int": the text was a number, but not a
whole one (
"2.5"). - "Enter a number. To let this be left blank...": the box was empty. The message says "no value was provided" where the other two say "got" and quote the text.
Then consider two underlying questions. Should this field only ever hold whole numbers, and may a visitor leave it empty?
Fix
Match the annotation to what the field means:
- A count or a whole quantity: keep
int, and considerTextBox("age", 0, "number"), whose"number"kind tells the browser to guide the visitor toward entering digits. - A measurement or money: annotate
floatinstead. - Free text that sometimes holds a number: annotate
strand convert yourself when it matters, checkingisdigit()first, as the Calculator example does. - A number that may be left empty: give the parameter a default, as
in
age: int = 0, and an empty box gives the default. Or annotate itage: int | None, and an empty box givesNone, which the route checks for. Blank values has the full rule.
@route
def set_price(state: State, price: float) -> Page:
...
Confirm
Submit the same text again; the route should now run. Add a test that
exercises the conversion, calling the route with a typed value:
set_price(State(...), 2.5).
Prevent
When you create a TextBox destined for a number, decide int or
float at the same moment you write the route's annotation, and
label the field so visitors know what to type ("Age in years:").
Understand
Forms and input covers the conversion table: which annotations convert, and what happens to each kind of typed value.