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

问题描述 投票:0回答:1

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

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

UndefinedError: 'form' is undefined

-波特.朋友

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
python forms flask
1个回答
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 %}
© www.soinside.com 2019 - 2024. All rights reserved.