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

Django DRF导入CustomJWTAuthentication类失败的问题求助

Django DRF导入CustomJWTAuthentication类失败的问题求助

各位好,我最近在用Django + DRF搭建一个REST认证API,今天刚启动项目工作,终端里突然弹出了这个导入错误,实在搞不懂为啥突然就出问题了,来求助大家!

终端里的错误信息如下:

ImportError: Could not import 'users.authentication.CustomJWTAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: Module "users.authentication" does not define a "CustomJWTAuthentication" attribute/class.

我的settings.py里的REST_FRAMEWORK配置是这样的:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'users.authentication.CustomJWTAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    ...
}

对应的/users/authentication.py文件里明明已经定义了CustomJWTAuthentication类,代码如下:

from django.conf import settings
from rest_framework_simplejwt.authentication import JWTAuthentication

class CustomJWTAuthentication(JWTAuthentication):
    def authenticate(self, request):
        try:
            header = self.get_header(request)

            if header is None:
                raw_token = request.COOKIES.get(settings.AUTH_COOKIE)
            else:
                raw_token = self.get_raw_token(header)

            if raw_token is None:
                return None

            validated_token = self.get_validated_token(raw_token)

            return self.get_user(validated_token), validated_token
        except:
            return None

我的运行环境是Ubuntu 24系统,用虚拟环境(venv)管理依赖,版本信息如下:

  • Python v3.12
  • Django v4.2
  • DRF v3.14
  • DRF SimpleJWT v5.4

真的摸不着头脑,之前项目运行都正常,今天突然就报这个导入错误了,有没有大佬能帮我排查下问题出在哪呀?

备注:内容来源于stack exchange,提问作者user3125823

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 08:43:07