jinja2.exceptions.UndefinedError:'str object'没有属性'username'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jinja2.exceptions.UndefinedError:'str object'没有属性'username'相关的知识,希望对你有一定的参考价值。
在尝试加载register.html页面时,我遇到了让我的烧瓶应用程序识别我的表单变量的问题。我试图重新加载我的virtualenv并试图从另一个人加载模板代码,我得到相同的错误。这是我得到的追溯。
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "s:projectsgameappenvlibsite-packagesflask\_compat.py", line 35, in reraise
raise value
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "s:projectsgameappenvlibsite-packagesflask\_compat.py", line 35, in reraise
raise value
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "s:projectsgameappenvlibsite-packagesflaskapp.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "S:Projectsgameappgameapp.py", line 35, in register
return render_template('register.html', title='Register', form='form')
File "s:projectsgameappenvlibsite-packagesflask emplating.py", line 135, in render_template
context, ctx.app)
File "s:projectsgameappenvlibsite-packagesflask emplating.py", line 117, in _render
rv = template.render(context)
File "s:projectsgameappenvlibsite-packagesjinja2asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "s:projectsgameappenvlibsite-packagesjinja2environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "s:projectsgameappenvlibsite-packagesjinja2environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "s:projectsgameappenvlibsite-packagesjinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "S:Projectsgameapp emplates
egister.html", line 1, in top-level template code
{% extends "layout.html" %}
File "S:Projectsgameapp emplateslayout.html", line 44, in top-level template code
{% block content %}{% endblock %}
File "S:Projectsgameapp emplates
egister.html", line 9, in block "content"
{{ username.label(class="form-control-label") }}
File "s:projectsgameappenvlibsite-packagesjinja2environment.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'时出错,我认为它与版本错误有关,因为一切正常,除了这个(目前)。任何帮助,将不胜感激。
答案
您需要使用“class_”属性来标记,而不是“类”,这意味着您需要修复
{{ form.username.label(class="form-control-label") }}
至
{{ form.username.label(class_="form-control-label") }}
等等整个模板。
另一答案
用户@Andellys提到,您应该从class
更改为class_
,但也可能没有实例化您的表单。请检查这个答案:hidden_tag() missing 1 required positional argument: 'self' in flask forms?。
以上是关于jinja2.exceptions.UndefinedError:'str object'没有属性'username'的主要内容,如果未能解决你的问题,请参考以下文章