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

Django集成dj-rest-auth启动时报NameError: name 'dj_rest_auth'未定义

问题原因及解决方案

1. 直接错误原因:urls.py缺少模块导入

你当前的urls.py中仅导入了admin、path、include三个对象,没有导入dj_rest_auth模块,所以调用dj_rest_auth.urls时会触发未定义变量的报错。
两种修复方案选其一即可:

  • 方案一:在urls.py顶部新增导入语句
    import dj_rest_auth
    
  • 方案二:将include参数改为字符串路径,无需额外导入模块
    把path('customer/dj-rest-auth/', include(dj_rest_auth.urls)),改为:
    path('customer/dj-rest-auth/', include('dj_rest_auth.urls')),
    

修正后的完整urls.py示例(方案一)

from django.contrib import admin
from django.urls import path, include
import dj_rest_auth

urlpatterns = [
    path('admin/', admin.site.urls),
    
    path('customer/', include('customer.urls')), #new
    path('api-auth/', include('rest_framework.urls')), # new
    path('customer/dj-rest-auth/', include(dj_rest_auth.urls)),
]

2. 隐藏风险:依赖包冲突

你的requirements.txt中同时安装了仍在维护的dj-rest-auth和已经停止维护的旧版本django-rest-auth,两个包功能高度重叠,后续大概率会出现命名冲突、功能异常等问题,建议直接卸载旧包:

pip uninstall django-rest-auth -y

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:09:05