1
votes

I know there are many cases out there that throws this error, and I search really hard on stackoverflow and other sites but I could not figure out why form is keep being undefined. Any help will really be appreciated

I have wtf_input.py which have all the wtf defined like below

class InputForm(Form):

# individual_first_name: DataRequired, is_name_non_digit, is_name_length, is_english
first_name = StringField("First Name", [validators.DataRequired("Please enter first name"),
                                                           I.is_name_non_digit, I.is_name_length, I.is_english])

# individual_last_name: DataRequired, is_name_non_digit, is_name_length, is_english
last_name = StringField("Last Name", [validators.DataRequired("Please enter last name"),
                                                           I.is_name_non_digit, I.is_name_length, I.is_english])

# date_of_birth:
date_of_birth = DateField('date_of_birth', [validators.DataRequired('Birth date is required')], format='%Y-%m-%d',
                          description='Date format: YYYY-MM-DD', default='2001-02-02')
   .
   .
   . a lot more 

I is the class(calling static method) where my custom validator class is at.

my view/test.py (where all my view is at)

from app.views.wtf_input import InputForm
@test_blueprint.route('/')
def index():

   return render_template('wtf_input.html')


@test_blueprint.route('/post_user', methods=['GET', 'POST'])
def post_user():

   form = InputForm()

   if form.validate():
       return render_template('wtf_input.html', form=form)

This is giving me the below error

Traceback (most recent call last)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/genomics/PycharmProjects/SA_test/app/views/post_inputs.py", line 62, in index
return render_template('wtf_input.html')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template
context, ctx.app)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/templating.py", line 116, in _render
rv = template.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/jinja2/environment.py", line 989, in render
return self.environment.handle_exception(exc_info, True)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/jinja2/environment.py", line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/Users/genomics/PycharmProjects/SA_test/app/templates/wtf_input.html", line 9, in top-level template code
{% for message in form.name.errors %}
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/jinja2/environment.py", line 408, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'form' is undefined

And I really cannot figure out what is going on.

Can someone please help me out on this?

Thanks

1

1 Answers

2
votes

Your index() is generating this error. As you define form in post_user action and you're passing it to the template, your index action is using the same template wtf_input.html but you're not passing any form definition in this action.