gpt4 book ai didi

python - Django:调整@login_required 装饰器

转载 作者:太空狗 更新时间:2023-10-29 17:38:37 24 4
gpt4 key购买 nike

我想为我的网站开始私有(private)测试版。我有一个初始页面,用户可以在其中输入代码以访问网站的其余部分。目前,所有其他网站页面(启动页面除外)都包含一系列通过要求用户登录(通过@login_required 装饰器)设置的重定向。

我希望登录用户和输入 Beta Tester 代码的人都能够访问站点的其余部分。这意味着我不能只对所有 View 使用装饰器。

我应该改变@login_required 装饰器本身吗?我更想做以下事情(如果用户在启动页面上输入正确的代码,我添加了一个 session 变量)。

def view_name(request):
user=request.user
if not user.id or not request.session.get('code_success'):
return HttpResponseRedirect('/splash/')

这看起来合理吗?我不想为了我的所有观点而重复它

布兰登

最佳答案

编写您自己的装饰器——非常简单。事实上,如果您查看 login_required 的 Django 源代码,您应该能够出于自己的目的随意摆弄一份副本。

def my_login_required(function):
def wrapper(request, *args, **kw):
user=request.user
if not (user.id and request.session.get('code_success')):
return HttpResponseRedirect('/splash/')
else:
return function(request, *args, **kw)
return wrapper

关于python - Django:调整@login_required 装饰器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5678585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com