相关疑难解决方法(0)

如何为Django/Python视图编写装饰器?

这是我的看法.基本上,它会根据是否登录返回不同的响应.

@check_login()
def home(request):
    if is_logged_in(request): 
        return x
    else:
        return y
Run Code Online (Sandbox Code Playgroud)

这是我的装饰代码.我只想检查请求是否有标题,如果是,请将其登录.

#decorator to log the user in if there are headers
def check_login():
    def check_dec(func):
        if request.META['username'] == "blah":
            login(request, user)

    return check_dec
Run Code Online (Sandbox Code Playgroud)

问题是..在这种情况下,我不知道如何写一个合适的装饰器!有什么争论?有什么功能?怎么样?

python django http decorator request

4
推荐指数
1
解决办法
4716
查看次数

标签 统计

decorator ×1

django ×1

http ×1

python ×1

request ×1