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

Django导入错误:无法导入'patterns',URL配置问题求助

Fixing Django's ImportError: cannot import name 'patterns'

Hey there! Let's sort out this import error you're facing. The issue here is super straightforward: Django removed the patterns function starting from version 1.10—it was deprecated in 1.8 and fully removed later, so trying to import it from django.conf.urls will throw that error.

Here's how to fix your mysite/urls.py:

  1. Remove the patterns import
    Update your import line to get rid of patterns entirely. Your imports should look like this:

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

    (If you prefer using the newer syntax, you can import re_path instead of url—they're functionally the same, with url being an alias for re_path.)

  2. Rewrite the login URL entry
    The old patterns('login.views', ...) syntax is no longer supported. Instead, follow the same pattern you're using for your other apps (like polls, user-auth) and use include to point to your login app's urls.py file. If you haven't created login/urls.py yet, make that file first—this keeps your project organized.

    Your updated urlpatterns should look like this:

    urlpatterns = [
        path('admin/', admin.site.urls),
        url(r'^polls/', include('polls.urls')),
        url(r'^user-auth/', include('user_auth.urls')),
        url(r'^file-upload/', include('file_uploader.urls')),
        url(r'^pagination/', include('pagination.urls')),
        # Updated login URL using include (matches your other app setup)
        url(r'^login/', include('login.urls')),
    ]
    

    If you want to point directly to a view without using include, just import the login views and reference them directly:

    # Add this import at the top
    from login import views
    
    # Then in urlpatterns:
    url(r'^login/', views.your_login_view_name),
    

Bonus: Using newer Django syntax (3.x+)

If you're on a newer Django version, it's recommended to use re_path for regex-based URLs (instead of url) and path for simple, non-regex paths. Your file would look like this:

from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
from login import views

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^polls/', include('polls.urls')),
    re_path(r'^user-auth/', include('user_auth.urls')),
    re_path(r'^file-upload/', include('file_uploader.urls')),
    re_path(r'^pagination/', include('pagination.urls')),
    re_path(r'^login/', include('login.urls')),
    # Or direct view reference:
    # re_path(r'^login/', views.your_login_view_name),
]

Just double-check your Django version with python -m django --version to make sure your syntax aligns with what your version supports.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:00:03