Flask / WTF表单将变量传递给模板会产生500错误,表单未定义

时间:2019-04-07 13:36:08

标签: python forms flask

我一直试图将WTForms表单中的变量传递到模板中,但它却给我带来了500个错误。我基本上是想在结果页面上显示前一个表单的用户输入。

但是我找到的所有解决方案都说我需要声明form = form,但是我已经做到了。这是results.html给出的错误,但我不确定为什么未定义表单。

UndefinedError: 'form' is undefined

-bot.py

from flask import Flask, render_template, flash, redirect, url_for
from config import Config
from forms import LoginForm
from flask_bootstrap import Bootstrap
from tweetscript import get_tweets

app = Flask(__name__)
app.config.from_object(Config)
bootstrap = Bootstrap(app)

@app.route('/', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        flash('Twitter name submitted {}'.format(form.username.data))
            return redirect(url_for('results'))
    return render_template('login.html', title='Home', form=form)

@app.route("/results")
def results():
    return render_template('results.html', title='Results')


if __name__ == "__main__":
    app.debug = True
    app.run(host='0.0.0.0')

-results.html

{% extends 'index.html' %}

{% block content %}
    <h1> results page, {{ form.username.data }}
     </h1>
{% endblock %}

-回溯

    Traceback (most recent call last):
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "./bot.py", line 21, in results
    return render_template('results.html', title='Results')
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask/templating.py", line 135, in render_template
    context, ctx.app)
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask/templating.py", line 117, in _render
    rv = template.render(context)
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/pi/bot/project/templates/results.html", line 1, in top-level template code
    {% extends 'index.html' %}
  File "/home/pi/bot/project/templates/index.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/home/pi/bot/project/templates/base.html", line 40, in top-level template code
    {% for message in messages %}
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
    {% block doc -%}
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
    {%- block html %}
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
    {% block body -%}
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 23, in block "body"
    {% block content -%}
  File "/home/pi/bot/project/templates/results.html", line 6, in block "content"
    {{ form.username.data }}
  File "/home/pi/bot/project/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
UndefinedError: 'form' is undefined

1 个答案:

答案 0 :(得分:0)

错误来自以下事实:您的results函数仅返回两件事:页面 results.html 及其标题;但是在您的页面 results.html 中,您尝试恢复该功能中未定义但来自另一个功能的数据(登录)。

一种可能的解决方案是:

...
@app.route('/', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        flash('Twitter name submitted {}'.format(form.username.data))
            return redirect(url_for('results', result_data = form.username.data))
    return render_template('login.html', title='Home', form=form)

@app.route("/results/<result_data>")
def results(result_data=None):
    return render_template('results.html', title='Results', result=result_data)
...

并在您的 results.html 页面中:

{% extends 'index.html' %}

{% block content %}
    <h1> results page, {{ result }} </h1>
{% endblock %}