Skip to content
Advertisement

Using uk-postcode-utils in a python form

I am creating a form for the user to enter a valid UK postcode. I currently have the following code for the form:

JavaScript

and the template is:

JavaScript

but getting the error: TypeError: argument of type ‘StringField’ is not iterable.

What can I do to fix this?

Edit: Here are the complete form entry and the template for the webpage. Thanks a lot.

Advertisement

Answer

You are not using the WTForms validators correctly in your postcode StringField.

Declare a function that takes form and field parameters and raise an error if the post code is invalid, in this function you make use of the uk-postcode-utils is_valid_postcode function, i.e.:

JavaScript

Register the validator on the StringField:

JavaScript

If a post code field is being used in several forms you’re better off writing a class validator as outlined in the WTForms documentation.

Something like (untested):

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement