python - 使 Flask-Login 的 login_required 成为默认值的最佳方法

标签 python flask

喜欢这个问题:Best way to make Django's login_required the default

我正在使用 Flask-Loginlogin_required现在装修。有没有让它成为 Flask 中的默认行为?

最佳答案

我在我的 instruments 中这样做了项目。我使用 before_request 装饰器:

@app.before_request
def check_valid_login():
    login_valid = 'user' in session # or whatever you use to check valid login

    if (request.endpoint and 
        'static' not in request.endpoint and 
        not login_valid and 
        not getattr(app.view_functions[request.endpoint], 'is_public', False) ) :
        return render_template('login.html', next=request.endpoint)

然后我为几个无需登录即可访问的地方创建了一个 is_public() 装饰器:

def public_endpoint(function):
    function.is_public = True
    return function

关于python - 使 Flask-Login 的 login_required 成为默认值的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13428708/

相关文章:

python - Pyplot set_xticks 无法按预期工作

python - 使用 NLTK 自定义 POS 标记(错误)

python - "def __iter__(self): return iter(self.file)"是做什么用的?

python - Flask:手动保存 session 数据

python - 如何在 Postman 中上传文件?

python - 杀死在 Python 类 __init__ 中创建的子进程

java 相当于 python syslog.syslog()

python - newrelic 代理仅在登台时不向 newrelic 服务器发送数据

flask - 'primaryjoin' 和 'secondaryjoin' 如何处理 SQLAlchemy 中的多对多关系?

python - peewee DoubleField 的默认值不会反射(reflect)在 MySQL 数据库中