使用ORM peewee,一直关注烧瓶博客文章类型app的用户注册和登录表单的Flask安全协议。虽然注册存在问题,登录就像魅力一样。 相关问题How to use Flask-Security register view?
应用程序:
from flask import render_template, request, redirect, url_for
from models import *
@app.route('/')
def home():
return render_template('home.html',
posts=Post.select().order_by(Post.date.desc()))
@app.route('/login', methods=['GET', 'POST'])
def login():
return render_template('/security/login_user.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
return render_template('/security/register_user.html')
数据库处理程序:
from flask import Flask
from flask_peewee.db import Database
from peewee import *
from flask_security import Security, PeeweeUserDatastore, UserMixin,
RoleMixin, login_required
import datetime
# Create app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'super-secret'
app.config['DATABASE'] = {
'name': 'unnamed.db',
'engine': 'peewee.SqliteDatabase',
}
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_REGISTER_URL'] = '/register'
app.config['SECURITY_LOGIN_USER_TEMPLATE'] = 'security/login_user.html'
app.config['SECURITY_REGISTER_USER_TEMPLATE'] =
'security/register_user.html'
app.config['SECURITY_RESET_PASSWORD_TEMPLATE'] =
'security/reset_password.html'
app.config['SECURITY_CHANGE_PASSWORD_TEMPLATE'] =
'security/change_password.html'
# Database connection object
db = Database(app)
user_datastore = PeeweeUserDatastore(db, User, Role, UserRoles)
security = Security(app, user_datastore)
注册用户表单(通常从其git获取的Flask_Security文件):
{% from "security/_macros.html" import render_field_with_errors,
render_field %}
{% include "security/_messages.html" %}
<h1>{{ ('Register') }}</h1>
<form action="{{ url_for_security('register') }}" method="POST"
name="register_user_form">
{{ register_user_form.hidden_tag() }}
{{ render_field_with_errors(register_user_form.email) }}
{{ render_field_with_errors(register_user_form.password) }}
{% if register_user_form.password_confirm %}
{{ render_field_with_errors(register_user_form.password_confirm) }}
{% endif %}
{{ render_field(register_user_form.submit) }}
</form>
{% include "security/_menu.html" %}
日志在渲染过程中显示输出:
带有寄存器退格的URL正在与登录进行对比。已经检查了整个事情几个小时,但无济于事。可能会发生什么暗示?
答案 0 :(得分:3)
而不是:
@app.route('/register', methods=['GET', 'POST'])
你需要:
@app.route('/register/', methods=['GET', 'POST'])
请注意'/ register /'
末尾的斜杠/如果“(...)定义的URL没有斜杠,而是类似UNIX类系统上文件的路径名。使用尾部斜杠访问URL将产生404”未找到“错误”。
答案 1 :(得分:1)
除了在创建url请求时包含尾部斜杠的已发布解决方案(根据Flask Doc必须执行此操作)之外,上述问题是由于缓存未被清除引起的。 Related question将通过硬重载更深入地了解清除缓存。或者,只需在开发模式下禁用缓存。