You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django添加登录限制装饰器后报view must be a callable错误求助

问题原因

你编写的unauthenticated_user装饰器存在语法错误,没有返回内层包装函数,导致被装饰的loginPage视图最终为None,不符合Django要求的视图必须是可调用对象的规则。

解决方案

  • 修正decorators.py中的代码,在wrapper_func定义结束后,外层的unauthenticated_user函数要返回这个包装函数,修正后的代码如下:
from django.http import HttpResponse
from django.shortcuts import redirect

def unauthenticated_user(view_func):
    def wrapper_func(request, *args, **kwargs):
        if request.user.is_authenticated:
            return redirect('home')
        else:
            return view_func(request, *args, **kwargs)
    # 新增return语句返回包装函数
    return wrapper_func
  • 修正代码后重新启动Django服务,报错即可消除。

内容的提问来源于stack exchange,提问作者Lucas Koblitz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 19:27:05