使用Flask-Security表单未定义

时间:2017-08-23 14:07:13

标签: python flask wtforms flask-wtforms flask-security

我正在使用Flask-Security和我的登录表单的自定义模板。我已经定义了我的表单,但无论出于何种原因,表单都没有传递给模板。

这是表单代码

class LoginForm(FlaskForm):
    email = TextField('Email Address', [
            Required(message='Forgot your email address?')])
    password = PasswordField('Password', [
            Required(message='Must provide a password. ;-)')])

这是模板代码

<form method="POST" action="." accept-charset="UTF-8" role="form">
    {{ form.csrf_token }} {{ render_field(form.email, placeholder="Your Email Address", autofocus="") }} {{ render_field(form.password, placeholder="Password") }}
        <div>
            <label>
                        <input type="checkbox" name="remember" value="1"> Remember Me
                    </label>
                        <a role="button" href="">Forgot your password?</a><span class="clearfix"></span>
        </div>
                    <button type="submit" name="submit">Sign in</button>        
</form>

以下是登录的代码

@mod_auth.route('/signin/', methods=['GET', 'POST'])
def signin():
    # If sign in form is submitted
    form = LoginForm(request.form)
    # Verify the sign in form
    if form.validate_on_submit():
        for user in User.objects(email=form.email.data):
            if user and user.password == form.password.data:
                login_user(user)
            if not is_safe_url(next):
                flash('Logging you in')
                return flask.abort(400)
            return redirect(next or ('/')
        elif user:
            flash('Wrong password')
        else:
            flash('could not find username')
    return render_template("security/login_user.html", form=form)

如果删除配置

中的行,代码就可以工作
app.config['SECURITY_LOGIN_URL'] = '/auth/signin/'

但是当那条线出现时会抛出此错误

Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
 File "/usr/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/lib/python3.6/site-packages/flask_security/decorators.py", line 230, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/flask_security/views.py", line 89, in login
**_ctx('login'))
File "/usr/lib/python3.6/site-packages/flask_security/core.py", line 528, in render_template
return render_template(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/flask/templating.py", line 134, in render_template
context, ctx.app)
File "/usr/lib/python3.6/site-packages/flask/templating.py", line 116, in _render
rv = template.render(context)
File "/usr/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/kenton/Programming/bridal/bridal-website/becomeOne/app/templates/security/login_user.html", line 1, in top-level template code
{% extends "base.html" %} {% block content %}
File "/home/kenton/Programming/bridal/bridal-website/becomeOne/app/templates/base.html", line 1, in top-level template code
{% block doc -%}
File "/home/kenton/Programming/bridal/bridal-website/becomeOne/app/templates/base.html", line 4, in block "doc"
{%- block html %}
File "/home/kenton/Programming/bridal/bridal-website/becomeOne/app/templates/base.html", line 43, in block "html"
{% block body -%}
File "/home/kenton/Programming/bridal/bridal-website/becomeOne/app/templates/base.html", line 48, in block "body"
{% block content -%} {%- endblock content %} {% block scripts %}
File "/home/kenton/Programming/bridal/bridal-website/becomeOne/app/templates/security/login_user.html", line 22, in block "content"
{% endif %} {% endwith %} {% if form.errors %}
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'form' is undefined

任何想法或帮助都会很棒! 提前谢谢。

1 个答案:

答案 0 :(得分:-1)

Jinja2抛出错误:

jinja2.exceptions.UndefinedError: 'form' is undefined

上面的模板代码使用的是form,它并不知道。您不会将其提供给使用它的模板。您必须将form传递给使用它的模板。