【问题标题】:DJ_REST_AUTH Error : NameError: name 'dj_rest_auth' is not definedDJ_REST_AUTH 错误:NameError:未定义名称“dj_rest_auth”
【发布时间】:2021-11-14 04:27:51
【问题描述】:

我已经安装了dj-rest-auth用于令牌认证。

我的 urls.py:

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

    
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)),
]

Settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'customer',
    'rest_framework.authtoken', # new
    'dj_rest_auth',
]

我的Requirements.txt:

asgiref==3.4.1
dj-rest-auth==2.1.11
Django==3.2.7
django-rest-auth==0.9.5
djangorestframework==3.12.4
psycopg2-binary==2.9.1
pytz==2021.1
six==1.16.0
sqlparse==0.4.2

但是当我尝试 runserver 它给了我

NameError:名称“dj_rest_auth”未定义

我做错了什么?

【问题讨论】:

    标签: django authentication django-rest-framework


    【解决方案1】:

    作为documented,您应该将其包含为字符串"dj_rest_auth.urls",而不是dj_rest_auth.urls,它无法从中找到定义dj_rest_auth 的位置:

    path('dj-rest-auth/', include('dj_rest_auth.urls'))
    

    这与 django.urls.include() 的用法一致,它期望它是模块名称。

    【讨论】:

    • 非常感谢。这真的很快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多