flask - 烧瓶,视图装饰器没有效果
问题描述
我的烧瓶应用程序中的视图装饰器遇到了一些问题。它们似乎根本没有效果。这是查看用户是否登录的基本方法:
# Decorator to ensure user must be logged in
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/signin")
return f(*args, **kwargs)
return decorated_function
如果我在没有登录的情况下启动我的服务器并尝试直接访问某个页面, http://127.0.0.1:5000/profile
我会得到一个Internal Server Error
. 我想我应该被重定向到 /signin 但事实并非如此。
这是我的第二个装饰器:
# Decorator to ensure user is confirmed via email
def check_confirmed(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if isConfirmed() == "no":
flash("Please enter secrect code sent to the given email address")
return redirect("/unconfirmed")
return f(*args, **kwargs)
return decorated_function
isConfirmed()
从我的数据库返回一个字符串并工作。但同样,它并没有将我重定向到/unconfirmed
我没有做的事情。
这两个函数都在同一个文件中,我确实 import from functools import wraps
. 我不知道我错过了什么,谁能发现错误?
如果需要我的代码中的更多信息,请随时告诉我。
非常感谢,我祝大家新年快乐!
解决方案
我需要一只橡皮鸭!
我忘了在我的路由功能下添加@login_required 和@check_confirmed。现在它可以例外地工作。
推荐阅读
- java - 使用 java 8 流获得最大平均科目分数
- python - 如何在 Jupyter Notebook 中正确设置 SparkContext 的配置?
- mongodb - 获取一个键/值对象,其中键是一个值的枚举
- java - getInputStream 字符限制
- algorithm - 逆波兰表示法中的可变长度运算符(后缀)
- docker - 使用 Dockerfile 中的唯一 COPY 语句将一些文件和整个文件夹从主机复制到容器中?
- selenium - 如何在 Windows 应用程序自动化测试中在窗口和上下文之间切换
- nuxt.js - 如何在 Nuxt 配置中设置 .env 文件路径?
- angular - 页面加载时禁用按钮
- deno - Deno:获取当前模块的目录