gpt4 book ai didi

python - 如何将两个装饰器中的任何一个应用到 View

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:08 25 4
gpt4 key购买 nike

我有两个装饰器函数 - authenticateauthenticate_remote。此时我必须以这种方式依次使用它们:

@authenticate
def view_local(request):
...

@authenticate_remote
def view_remote(request):
...

其中 view_localview_remote 具有完全相同的代码。我想要的是这样的:

@authenticate or @authenticate_remote
def view_one(request):
...

在这种情况下,我不必复制粘贴太多,也不必到处复制类似的代码。

最佳答案

这是我会做的:

def view_one(request):
# common code

view_local = authenticate(view_one)
view_remote = authenticate_remote(view_one)

它简单、明确,并且将逻辑排除在 urls 模块之外。

关于python - 如何将两个装饰器中的任何一个应用到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30807445/

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