Python 装饰师要求使用doens';我的样品在烧瓶中不能正常工作
我对flask主体中的decorator require(http_exception=401)有问题。我试图访问该网站,需要登录,但我能够访问那里。你知道为什么吗?我的代码如下: infrastructure.pyPython 装饰师要求使用doens';我的样品在烧瓶中不能正常工作,python,authentication,flask,flask-principal,Python,Authentication,Flask,Flask Principal,我对flask主体中的decorator require(http_exception=401)有问题。我试图访问该网站,需要登录,但我能够访问那里。你知道为什么吗?我的代码如下: infrastructure.py app = Flask(__name__) db = SQLAlchemy(app) principals = Principal(app) principals._init_app(app) # User Information providers @identity_load
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')
视图/profile.py
@member_perm.require(http_exception=401)
@app.route('/profil/', methods=['GET', 'POST'])
def profil():
# code ...
谢谢您的建议。装饰者应位于
@app.route
装饰者的下方/内部:
因此,请尝试:
@app.route('/profil/', methods=['GET', 'POST'])
@member_perm.require(http_exception=401)
def profil():
# code ...