Skip to content
Advertisement

How to call a variable outside a validate() function which is inside the FlaskForm class

I have two def validate(self) functions within the RegistrationForm(FlaskForm). First function validates the user input if the car range is over 400km. The other function validates if the user lives far or close to work by calculating the distance between the user’s address and work address by means of geopy module. I’ve been told that def validate_field(self, field): function takes only one attribute which must be the same as the field name. In that function I have an extra variable total_travel_km that stores distance between work and user’s home. If user is validated than I need to store total_travel_km variable in DB.

Now the server brings me this message: ‘RegistrationForm’ object has no attribute ‘total_travel_km’

How can I call that total_travel_km variable correctly and write it inside DB

Here is the cut out from my code:

JavaScript

EDIT: I figured out. I had to pass form. instead of self. in validation method. Below is the correct code:

JavaScript

Advertisement

Answer

Set a form instance variable in the validation method:

JavaScript

Also, you might not want to hard code the car range limit, you can pass a value when you instance the form:

JavaScript

and use as follows:

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