Reputation: 55
I have a very simple flask form setup with 1 field. This field is passed via a POST Request to a script that runs and returns a render_template to the results page passing 4 list objects from the script.
I can run the script in the python interpreter and print the values of the lists. But, it doesn't seem to be passing the lists back to the view. I keep getting a form undefined error. I think it is just skipping down to the return function in the index route instead of returning the view from the function. Do I need to do something to the list variables before sending to the view?
Route
@app.route('/', methods=['GET', 'POST'])
def index():
form = Search()
if form.validate_on_submit():
name=request.form['name']
smoothSearch(name)
return render_template("index.html", form=form)
Function
#function runs a for loop appending items to each list item then returns a render_template shown below
else:
return render_template("results.html", usernames=usernames, userHandle=userHandle, userText=userText, postTime=postTime)
EDIT to include jinja2 trackback below
jinja2.exceptions.UndefinedError jinja2.exceptions.UndefinedError: 'form' is undefined
Traceback (most recent call last) File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1997, in call return self.wsgi_app(environ, start_response)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app response = self.handle_exception(e)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception reraise(exc_type, exc_value, tb)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise raise value
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request()
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise raise value
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request()
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint
File "/home/user/temp/code-projects/smoothSearch/app.py", line 22, in index smoothSearch(name)
File "/home/user/temp/code-projects/smoothSearch/smoothSearch.py", line 31, in smoothSearch return render_template('results.html', usernames=usernames, userHandle=userHandle, userText=userText, postTime=postTime)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template context, ctx.app)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/templating.py", line 116, in _render rv = template.render(context)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render return self.environment.handle_exception(exc_info, True)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception reraise(exc_type, exc_value, tb)
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise raise value.with_traceback(tb)
File "/home/user/temp/code-projects/smoothSearch/templates/results.html", line 1, in top-level template code {% extends "index.html" %}
File "/home/user/temp/code-projects/smoothSearch/templates/index.html", line 17, in top-level template code {{ form.csrf_token }}
File "/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/environment.py", line 430, in getattr return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'form' is undefined
Upvotes: 1
Views: 974
Reputation: 1744
Assuming your Function is the end of smoothSearch(...)
, you need a return statement in front of your function call.
@app.route('/', methods=['GET', 'POST'])
def index():
form = Search()
if form.validate_on_submit():
name=request.form['name']
return smoothSearch(name)
return render_template("index.html", form=form)
Edit after question update:
From your traceback you can see that on line 17 of index.html
there is a call to form.csrf_token
. Since you aren't passing a form
to your template, it is throwing your error. Removing that from your index.html
will solve this error but it will most likely break any forms you have. You'll have to go add the form.csrf_token
back into all your forms.
Upvotes: 2