WTForms:同一页上有两个表单吗?

我有一个动态网页,该网页应处理两种形式:登录表单和注册表单。我正在使用WTForms处理这两种形式,但是由于将两种形式都呈现到同一页面上,因此使它无法正常工作。

以下是我的网页登录表单的代码:

PYTHON:

class Login(Form):

login_user = TextField('Username', [validators.Required()])

login_pass = PasswordField('Password', [validators.Required()])

@application.route('/index', methods=('GET', 'POST'))

def index():

l_form = Login(request.form, prefix="login-form")

if request.method == 'POST' and l_form.validate():

check_login = cursor.execute("SELECT * FROM users WHERE username = '%s' AND pwd = '%s'"

% (l_form.login_user.data, hashlib.sha1(l_form.login_pass.data).hexdigest()))

if check_login == True:

conn.commit()

return redirect(url_for('me'))

return render_template('index.html', lform=l_form)

HTML:

<form name="lform" method="post" action="/index">

{{ lform.login_user }}

{{ lform.login_pass }}

<input type="submit" value="Login" />

</form>

以下是我的网页注册表格的代码:

PYTHON:

class Register(Form):

username = TextField('Username', [validators.Length(min=1, max = 12)])

password = PasswordField('Password', [

validators.Required(),

validators.EqualTo('confirm_password', message='Passwords do not match')

])

confirm_password = PasswordField('Confirm Password')

email = TextField('Email', [validators.Length(min=6, max=35)])

@application.route('/index', methods=('GET','POST'))

def register():

r_form = Register(request.form, prefix="register-form")

if request.method == 'POST' and r_form.validate():

check_reg = cursor.execute("SELECT * FROM users WHERE username = '%s' OR `e-mail` = '%s'"

% (r_form.username.data, r_form.email.data))

if check_reg == False:

cursor.execute("INSERT into users (username, pwd, `e-mail`) VALUES ('%s','%s','%s')"

% (r_form.username.data, hashlib.sha1(r_form.password.data).hexdigest(), check_email(r_form.email.data)))

conn.commit()

return redirect(url_for('index'))

return render_template('index.html', rform=r_form)

HTML:

<form name="rform" method="post" action="/index">

{{ rform.username }}

{{ rform.email }}

{{ rform.password }}

{{ rform.confirm_password }}

<input type="submit" value="Register />

</form>

继续加载网页时出现以下错误:

    Traceback (most recent call last):

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1820, in wsgi_app

response = self.make_response(self.handle_exception(e))

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1403, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1817, in wsgi_app

response = self.full_dispatch_request()

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request

rv = self.handle_user_exception(e)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1381, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request

rv = self.dispatch_request()

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\app.py", line 1461, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "C:\Users\HTVal_000\Desktop\innoCMS\main.py", line 36, in index

return render_template('index.html', lform=l_form)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\templating.py", line 128, in render_template

context, ctx.app)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\flask\templating.py", line 110, in _render

rv = template.render(context)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\jinja2\environment.py", line 969, in render

return self.environment.handle_exception(exc_info, True)

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\jinja2\environment.py", line 742, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Users\HTVal_000\Desktop\innoCMS\templates\default\index.html", line 52, in top-level template code

{{ rform.username }}

File "C:\Users\HTVal_000\Desktop\innoCMS\virtualenv\lib\site-packages\jinja2\environment.py", line 397, in getattr

return getattr(obj, attribute)

UndefinedError: 'rform' is undefined

据我了解,表单之间存在冲突,因为根据追溯:

return render_template('index.html', lform=l_form)

返回以下错误:

UndefinedError: 'rform' is undefined

当脚本看到:

{{ rform.username }}

{{ rform.email }}

{{ rform.password }}

{{ rform.confirm_password }}

但是它完全忽略了:

{{ lform.login_user }}

{{ lform.login_pass }}

这可能有点令人困惑,我也对负载感到困惑,并且我希望有人之前曾经遇到过这个问题,以便我也可以解决它。

回答:

这有点令人困惑,因为你在index()和register()上都渲染了index.html,并且都注册了相同的路由(@application.route('/index'))。当你向提交表单时/index,只会调用其中之一。你可以

  • 将所有逻辑都放在一个索引函数中,看看哪种形式(如果有)有效。
  • 分开逻辑,只提交相关表格

    通常,即使你希望在同一页面上同时显示登录名和注册名,也要分开逻辑。因此,我将尝试向你展示正确的方向:-)

例如,首先将你的登录和注册视图分开,这将仅检查与它们有关的表单的逻辑:

class Login(Form):

login_user = TextField('Username', [validators.Required()])

login_pass = PasswordField('Password', [validators.Required()])

class Register(Form):

username = TextField('Username', [validators.Length(min=1, max = 12)])

password = PasswordField('Password', [

validators.Required(),

validators.EqualTo('confirm_password', message='Passwords do not match')

])

confirm_password = PasswordField('Confirm Password')

email = TextField('Email', [validators.Length(min=6, max=35)])

@application.route('/login', methods=['POST'])

def index():

l_form = Login(request.form, prefix="login-form")

if l_form.validate():

check_login = cursor.execute("SELECT * FROM users WHERE username = '%s' AND pwd = '%s'"

% (l_form.login_user.data, hashlib.sha1(l_form.login_pass.data).hexdigest()))

if check_login == True:

conn.commit()

return redirect(url_for('me'))

return render_template('index.html', lform=l_form, rform=Register())

@application.route('/register', methods=['POST'])

def register():

r_form = Register(request.form, prefix="register-form")

if r_form.validate():

check_reg = cursor.execute("SELECT * FROM users WHERE username = '%s' OR `e-mail` = '%s'"

% (r_form.username.data, r_form.email.data))

if check_reg == False:

cursor.execute("INSERT into users (username, pwd, `e-mail`) VALUES ('%s','%s','%s')"

% (r_form.username.data, hashlib.sha1(r_form.password.data).hexdigest(), check_email(r_form.email.data)))

conn.commit()

return redirect(url_for('index'))

return render_template('index.html', lform=Login(), rform=r_form)

@application.route('/index')

def index():

# If user is logged in, show useful information here, otherwise show login and register

return render_template('index.html', lform=Login(), rform=Register())

然后,创建一个显示两种形式的index.html,并按照正确的方向发送它们。

<form name="lform" method="post" action="{{ url_for('login') }}">

{{ lform.login_user }}

{{ lform.login_pass }}

<input type="submit" value="Login" />

</form>

<form name="rform" method="post" action="{{ url_for('register') }}">

{{ rform.username }}

{{ rform.email }}

{{ rform.password }}

{{ rform.confirm_password }}

<input type="submit" value="Register" />

</form>

该代码未经测试,因此可能存在错误,但是我希望它能向正确的方向发送信息。请注意,在所有对render(’index.html’,…)的调用中,我们同时传递了lform和rform。

改进/重构的其他简便方法:使用函数检查现有用户(你的SELECT陈述),并对模板中的各个表单使用Jinja2的包含或宏。

以上是 WTForms:同一页上有两个表单吗? 的全部内容, 来源链接: utcz.com/qa/410425.html

回到顶部