烧瓶wtforms无法通过表单-UndefinedError:'form'未定义

时间:2018-07-12 14:38:15

标签: python flask-wtforms

确定我的第一篇文章,所以我会尝试做的透彻。我相信根据其他一些帖子,我也有这个正确的说法,但是我仍然会收到未定义的错误。我想加载用数据库中提取的值填充的SelectMultipleField。用户应该能够选择他们想要包括在结果中的字段,并将其传递回系统。我已经包括了类的定义,路线,.html和我的助手。

    Entered in __init__.py with fields attribute defined globally

class renderselect(Form):
    elements = SelectMultipleField('Elements', choices = fields)

@app.route('//', methods=['GET', 'POST'])
def xmlsrcfields():
    try:
        # render the template
        form = renderselect(request.form)
        if request.method == 'POST':
            form.elements.choices = fields
            form.elements.process(request.form)
        else:
            #action to be completed for other cases
            gc.collect()

    except Exception as e:
        return(str(e)) code here

.html是

        <div class = "container">
            <h4>Convertable Elements</h4>
            <br>
            {% from "_formhelpers.html" import render_field %}
            <form method=post action="/fields/">
                <dl>
                    {{ render_field(form.elements) }}

                </dl>
                <p><input type=submit value=Convert></p>
            </form>

            {% if error %}
                <p class="error"><strong>Error:</strong>{{ error }}</p>
            {% endif %}

        </div>
    </div>

助手是

{% macro render_field(field) %}
    <dt>{{ field.label }}
    <dd>{{ field(**kwargs) |safe }}</dd>
    {% if field.errors %}
        <ul class=errors>
        {% for error in field.errors %}
            <li>{{ error }}</li>
        {% endfor %}
        </ul>
    {% endif %}
{% endmacro %}

根据要求进行完整的追溯:

[Thu Jul 12 10:45:14.887982 2018] [core:notice] [pid 11376:tid 1996320768] AH00094: Command line: '/usr/sbin/apache2'
[Thu Jul 12 10:45:35.357777 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613] ERROR:flask.app:Exception on /dashboard/ [GET], referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.357894 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613] Traceback (most recent call last):, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.357917 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2292, in wsgi_app, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.357938 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     response = self.full_dispatch_request(), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.357958 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1815, in full_dispatch_request, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.357978 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     rv = self.handle_user_exception(e), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.357997 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1718, in handle_user_exception, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358018 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     reraise(exc_type, exc_value, tb), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358036 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 35, in reraise, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358056 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     raise value, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358075 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1813, in full_dispatch_request, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358095 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     rv = self.dispatch_request(), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358114 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1799, in dispatch_request, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358134 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     return self.view_functions[rule.endpoint](**req.view_args), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358153 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 49, in dashboard, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358173 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     return render_template('dashboard.html', topic_dict = topic_dict), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358193 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/templating.py", line 135, in render_template, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358213 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     context, ctx.app), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358263 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/flask/templating.py", line 117, in _render, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358283 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     rv = template.render(context), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358300 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 1008, in render, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358319 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     return self.environment.handle_exception(exc_info, True), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358337 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 780, in handle_exception, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358356 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     reraise(exc_type, exc_value, tb), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358373 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/jinja2/_compat.py", line 37, in reraise, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358391 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     raise value.with_traceback(tb), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358408 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/var/www/FlaskApp/FlaskApp/templates/dashboard.html", line 1, in top-level template code, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358426 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     {% extends "headhome.html" %}, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358443 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/var/www/FlaskApp/FlaskApp/templates/headhome.html", line 138, in top-level template code, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358462 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     {% block body %}{% endblock%}, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358479 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/var/www/FlaskApp/FlaskApp/templates/dashboard.html", line 28, in block "body", referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358497 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     {{ render_field(form.elements) }}, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358514 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]   File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 430, in getattr, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358532 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613]     return getattr(obj, attribute), referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358555 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613] jinja2.exceptions.UndefinedError: 'form' is undefined, referer: http://192.168.1.115/login/
[Thu Jul 12 10:45:35.358588 2018] [wsgi:error] [pid 11378:tid 1955591216] [client 192.168.1.75:53613] , referer: http://192.168.1.115/login/

1 个答案:

答案 0 :(得分:0)

我完全不考虑渲染页面。感谢您的回复,尽管有人删除了他们的帖子,但我仍然感激不尽。

我只是在返回中添加了一个渲染器。自然,您看不到不是GLSurfaceView

的页面