0

我对烧瓶主体中的装饰器要求(http_exception = 401)有问题。我正在尝试访问需要登录的站点,但我可以访问那里。你知道为什么吗?我的代码如下:

基础设施.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')

视图/profile.py

@member_perm.require(http_exception=401)
@app.route('/profil/', methods=['GET', 'POST'])
def profil():
    # code ...

谢谢你的建议。

4

1 回答 1

1

@member_perm装饰器应该在装饰器的下方/内部@app.route

所以试试:

@app.route('/profil/', methods=['GET', 'POST'])
@member_perm.require(http_exception=401)
def profil():
    # code ...
于 2013-06-13T14:56:57.713 回答