ログインフォーム用のカスタムテンプレートでFlask-Securityを使用しています。私は自分のフォームを定義していますが、何らかの理由でフォームがテンプレートに渡されていません。ここで Flask-Securityを使用したフォームは未定義です
は、私が行を削除する場合@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)
コードが動作するログイン用のコードテンプレートコードここ
<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>
フォームコードここ
class LoginForm(FlaskForm):
email = TextField('Email Address', [
Required(message='Forgot your email address?')])
password = PasswordField('Password', [
Required(message='Must provide a password. ;-)')])
されています設定内
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
このエラーがスローされます任意の考えや、まったく助けには素晴らしいことです! ありがとうございます。
あなたはFlask-Securityを使用しています。なぜ自分でログインを処理するのですか? – stamaimer
@stamaimerよく私はセキュリティがログイン用のフラスコログインを使用していたことを知っていたので、ログインを行う方法については何も見たことがなかったので、フラスコのログインとセットアップに関するものはドキュメントに似ています。ログインを行うにはどうすればいいですか – Kenton
Flask-Securityの[クイックスタート](https://pythonhosted.org/Flask-Security/quickstart.html)を閲覧したことがありますか? – stamaimer