jinja2.exceptions.UndefinedError:'str object'没有属性'username'

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

在尝试加载register.html页面时,我遇到了让我的烧瓶应用程序识别我的表单变量的问题。我试图重新加载我的virtualenv并试图从另一个人加载模板代码,我得到相同的错误。这是我得到的追溯。

File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "s:\projects\gameapp\env\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "s:\projects\gameapp\env\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "s:\projects\gameapp\env\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "S:\Projects\gameapp\gameapp.py", line 35, in register
return render_template('register.html', title='Register', form='form')
File "s:\projects\gameapp\env\lib\site-packages\flask\templating.py", line 135, in render_template
context, ctx.app)
File "s:\projects\gameapp\env\lib\site-packages\flask\templating.py", line 117, in _render
rv = template.render(context)
File "s:\projects\gameapp\env\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "s:\projects\gameapp\env\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "s:\projects\gameapp\env\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "s:\projects\gameapp\env\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "S:\Projects\gameapp\templates\register.html", line 1, in top-level template code
{% extends "layout.html" %}
File "S:\Projects\gameapp\templates\layout.html", line 44, in top-level template code
{% block content %}{% endblock %}
File "S:\Projects\gameapp\templates\register.html", line 9, in block "content"
{{ username.label(class="form-control-label") }}
File "s:\projects\gameapp\env\lib\site-packages\jinja2\environment.py", line 430, in getattr
return getattr(obj, attribute)

这是我的registration.html页面

{% extends "layout.html" %}
{% block content %}
    <div class="content-section">
        <form method="POST" action="">
            {{ form.hidden_tag }}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Join Today</legend>
                <div class="form-group">
                    {{ form.username.label(class="form-control-label") }}
                    {{ form.username(class="form-control form-control-lg") }}
                </div>
                <div class="form-group">
                    {{ form.email.label(class="form-control-label")}}
                    {{ form.email(class="form-control form-control-lg")}}
                </div>
                <div class="form-group">
                    {{ form.password.label(class="form-control-label") }}
                    {{ form.password(class="form-control form-control-lg") }}
                </div>
                <div class="form-group">
                    {{ form.confirm_password.label(class="form-control-label") }}
                    {{ form.confirm_password(class="form-control form-control-lg") }}
                </div>
            </fieldset>
            <div class="form-group">
                {{ form.submit(class="btn btn-outline-info") }}
            </div>
        </form>
    </div>
    <div class="border-top pt-3">
        <small class="text-muted">
            Already Have An Account? <a class="ml-2" href="{{ url_for('login') }}">Sign In</a>
        </small>
    </div>
{% endblock content %}

我试图删除它引发错误的代码,我只是在下一行说没有属性'email'时出错,我认为它与版本错误有关,因为一切正常,除了这个(目前)。任何帮助,将不胜感激。

flask jinja2 flask-wtforms
2个回答
3
投票

您需要使用“class_”属性来标记,而不是“类”,这意味着您需要修复

{{ form.username.label(class="form-control-label") }}

{{ form.username.label(class_="form-control-label") }}

等等整个模板。


1
投票

用户@Andellys提到,您应该从class更改为class_,但也可能没有实例化您的表单。请检查这个答案:hidden_tag() missing 1 required positional argument: 'self' in flask forms?

© www.soinside.com 2019 - 2024. All rights reserved.