无法将WTForm字段的实例变量作为实例变量
这将具有多个(灵活的)字段,可以将其级联以增加字段的数量。
该程序对于一个简单的类,一个登录字段和一个密码字段(如果该字段是类变量)正常工作。
class LoginForm(FlaskForm):
username = StringField('Username')
password = PasswordField('Password')
如果在构造函数中初始化了字段,则程序无法正常工作。程序错误是
class TestLoginForm(FlaskForm):
def __init__(self):
username = StringField('Username')
password = PasswordField('Password')
以下是该程序的其他部分
<!-- project/app/templates/test.html -->
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Sign In</h1>
<form method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.username.label }}<br>
{{ form.username(size=32) }}
</p>
<p>
{{ form.password.label }}<br>
{{ form.password(size=32) }}
</p>
<p>{{ form.submit() }}</p>
</form>
</body>
</html>
from app import app
from flask import render_template
from app.forms import TestLoginForm
@app.route('/', methods=['GET','POST'])
@app.route('/login', methods=['GET','POST'])
def index():
form = TestLoginForm()
return render_template('test.html', form=form)
from flask import Flask
app = Flask(__name__)
app.config['SECRET_KEY'] = 'you-will-never-guess'
from app import routes
from app import app
请让我知道我需要学习哪些概念,并为获得问题结果的可能方法提供建议。
Traceback (most recent call last):
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\css120807\Documents\Flask_testWebsite\app\routes.py", line 13, in index
return render_template('test.html', form=form)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\templating.py", line 135, in render_template
context, ctx.app)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\templating.py", line 117, in _render
rv = template.render(context)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "C:\Users\css120807\Documents\Flask_testWebsite\app\templates\test.html", line 10, in top-level template code
{{ form.hidden_tag() }}
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_wtf\form.py", line 135, in hidden_tag
u'\n'.join(text_type(f) for f in hidden_fields(fields or self))
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_wtf\form.py", line 135, in <genexpr>
u'\n'.join(text_type(f) for f in hidden_fields(fields or self))
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_wtf\form.py", line 125, in hidden_fields
for f in fields:
File "C:\Users\css120807\AppData\Local\Continuum\anaconda3\lib\site-packages\wtforms\form.py", line 57, in __iter__
return iter(itervalues(self._fields))
AttributeError: 'TestLoginForm' object has no attribute '_fields'
答案 0 :(得分:0)
在快速入门中,有一个创建表单的示例: https://flask-wtf.readthedocs.io/en/stable/quickstart.html#creating-forms
有类字段,而不是实例(在__init__中)。
如果我理解正确,那么您想从父类继承字段,为什么不这样:
class MyForm(FlaskForm):
name = StringField('name', validators=[DataRequired()])
class AnotherForm(MyForm):
surname = StringField('surname', validators=[DataRequired()])
然后您可以在route.py中使用两种形式:
form = MyForm()
带有csrf令牌和名称字段
或
form = AnotherForm()
包含MyForm(csrf +名称)和姓氏中的所有内容