在尝试加载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”,我认为这与版本错误有关,因为一切正常除此之外(当前)。任何帮助将不胜感激。
答案 0 :(得分:3)
您需要使用“ class_”属性进行标记,而不是“ class”,这意味着您需要修复
heroku ps
heroku ps:stop run.5838 --app afternoon-bayou-10667
到
{{ form.username.label(class="form-control-label") }}
在整个模板中等等。
答案 1 :(得分:1)
如用户@ Andellys所述,您应该从class
更改为class_
,但是,也许您还没有实例化表单。请检查以下答案:hidden_tag() missing 1 required positional argument: 'self' in flask forms?。
答案 2 :(得分:0)
在解决错误之前,您需要先了解它。
在这里,错误明确指出str object
没有属性username
。这意味着使用名为username
的属性的对象是str
类型的(而它应该是表单类的对象)。
如果您还发布了表单的python代码,那会很容易,但是我仍然尝试找出问题所在。
假设您的表单类的名称为 Form ,即其声明如下:
class Form(FlaskForm):
然后在呈现registration.html的python文件中,您必须做类似的事情
form = Form()
我最好的猜测是,您已经将班级名称用引号引起来
forms = 'Forms()'
并且由于这个python将其解释为str对象而不是Form对象
因此只需删除实例化语句中的引号,您就可以开始使用
。希望有帮助:)