【问题标题】:ImportError: cannot import name 'url' from 'django.conf.urls' django-rest-authImportError:无法从 'django.conf.urls' django-rest-auth 导入名称 'url'
【发布时间】:2025-12-25 18:10:11
【问题描述】:

错误:从 django.conf.urls 导入 url ImportError: 无法从 'django.conf.urls' 导入名称 'url'

-版本 Django==4.0.1 django-rest-auth==0.9.5

Pl help me.Thank you in advance    

url.py

# Core Django imports
from django.contrib import admin
from django.urls import path, include
from django.urls import re_path,include
# from django.conf.urls import url,include
from django.conf import settings
# Rest framework imports
from rest_framework import permissions

# Simple JWT imports
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
    openapi.Info(
        title="Heathy Living Guide API",
        default_version='v1',
        description="Heathy Living Guide",
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

urlpatterns = (
    path('admin/', admin.site.urls),
    path('api/authentication/', include('apps.authentication.urls'),authentication'),
    path('api/users/', include('apps.users.urls'), name='users'),
    path('rest-auth/', include('rest_auth.urls')),
    # re_path(r'^rest-auth/', include('rest_auth.urls'))
)

urlpatterns += [
    path('api/swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('api/redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc')
]

【问题讨论】:

    标签: django url django-rest-framework importerror django-rest-auth


    【解决方案1】:

    django-rest-auth 不支持 django 4.0。 django-rest-auth 看起来被遗弃了,最后一次提交是在 3 年前。

    【讨论】:

      【解决方案2】:

      根据drf documentation

      Django-rest-auth 是原始项目,但目前不是 接收更新。

      Dj-rest-auth 是该项目的新分支。

      如果您仍想使用 django-rest-auth,您需要替换几个已弃用的 API 调用:

      • 对于 django.conf.urls 使用

        从 django.urls 导入 re_path 作为 url

      • 对于 ugettext 使用

        从 django.utils.translation 导入 gettext_lazy as _

      • 用于 force_text

        从 django.utils.encoding 导入 force_str 作为 force_text

      source

      【讨论】:

        最近更新 更多