我在flask-principal中有decorator require(http_exception = 401)的问题。我正在尝试访问需要登录的网站,但我可以访问那里。你知道为什么吗?我的代码如下:
infrastructure.py
app = Flask(__name__)
db = SQLAlchemy(app)
principals = Principal(app)
principals._init_app(app)
# User Information providers
@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
g.user = User.query.from_identity(identity)
# Permission
admin = Permission(RoleNeed('admin'))
member_perm = Permission(RoleNeed('member'))
User = get_user_class(db.Model)
视图/ login.py
# -*- coding: utf-8 -*-
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash, current_app
from flask.ext.principal import identity_changed, Identity
from hlidejkatastr.infrastructure import app, User
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
user = User.query.filter(User.email==email).first()
if user is not None:
if user.password == request.form['password']:
identity_changed.send(current_app._get_current_object(), identity=Identity(user.id))
return redirect(url_for('profil'))
else:
return render_template('login.html')
else:
return render_template('login.html')
return render_template('login.html')
视图/ profil.py
@member_perm.require(http_exception=401)
@app.route('/profil/', methods=['GET', 'POST'])
def profil():
# code ...
谢谢你的建议。
答案 0 :(得分:1)
@member_perm
装饰器应位于@app.route
装饰器下方/内:
所以试试:
@app.route('/profil/', methods=['GET', 'POST'])
@member_perm.require(http_exception=401)
def profil():
# code ...