80

Before I wrote in urls.py, my code... everything worked perfectly. Now I have problems - can't go to my site. "cannot import name patterns"

My urls.py is:

from django.conf.urls import patterns, include, url

They said what error is somewhere here.

brc
  • 5,281
  • 2
  • 29
  • 30
Autokilled
  • 843
  • 1
  • 6
  • 7
  • Can you please show us the full error and trace? – Tim Post Nov 10 '11 at 13:52
  • 3
    I had the same problem while following the "write your first django app" guide. When I moved the url patters from the main urls.py to polls.urls.py. The problem solved after I changed "from django.conf.urls import patterns, include, url" with "from django.conf.urls.defaults import * " – qliq Dec 14 '11 at 00:48
  • 2
    I had the same issue when deploying to my staging server. Problem went away went I upgraded from django 1.3.1 to 1.4 by running sudo pip install --upgrade django – Mike Grace Jun 13 '12 at 05:16

9 Answers9

162

As of Django 1.10, the patterns module has been removed (it had been deprecated since 1.8).

Luckily, it should be a simple edit to remove the offending code, since the urlpatterns should now be stored in a plain-old list:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    # ... your url patterns
]
Jacob Hume
  • 1,953
  • 2
  • 13
  • 14
  • 1
    but how can i append the `MEDIA_ROOT` and `MEDIA_URL` to the urlpatterns – Shift 'n Tab Sep 08 '16 at 05:42
  • 1
    How to add media url: https://docs.djangoproject.com/en/1.10/howto/static-files/#serving-static-files-during-development – Ahsan Oct 24 '16 at 03:30
  • 3
    For better documentation and rationale on this change in Django 1.8, see https://docs.djangoproject.com/en/1.10/releases/1.8/#django-conf-urls-patterns – nealmcb Dec 23 '16 at 15:16
  • And you must remove the blank URL `''` at the first position of `urlpatterns`, otherwise you'll get a Regex AttributeError. See https://stackoverflow.com/questions/34853531/attributeerror-str-object-has-no-attribute-regex-django-1-9#35875147 – Mr-IDE Feb 23 '18 at 20:54
30

You don't need those imports. The only thing you need in your urls.py (to start) is:

from django.conf.urls.defaults import *

# This two if you want to enable the Django Admin: (recommended)
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    # ... your url patterns
)

NOTE: This solution was intended for Django <1.6. This was actually the code generated by Django itself. For newer version, see Jacob Hume's answer.

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
21

Yes:

from django.conf.urls.defaults import ... # is for django 1.3
from django.conf.urls  import ...         # is for django 1.4

I met this problem too.

spikeyang
  • 691
  • 9
  • 17
12

patterns module is not supported.. mine worked with this.

from django.conf.urls import *
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    # ... your url patterns
]
Yatender Singh
  • 3,098
  • 3
  • 22
  • 31
6

This is the code which worked for me. My django version is 1.10.4 final

from django.conf.urls import url, include

from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    # Examples:
    # url(r'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
]
Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
4

Pattern module in not available from django 1.8. So you need to remove pattern from your import and do something similar to the following:

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

admin.autodiscover()

urlpatterns = [                 
    # here we are not using pattern module like in previous django versions
    url(r'^admin/', include(admin.site.urls)),
]
abidibo
  • 4,175
  • 2
  • 25
  • 33
Aiswarya
  • 41
  • 1
1

I Resolved it by cloning my project directly into Eclipse from GIT,

Initially I was cloning it at specific location on file system then importing it as existing project into Eclipse.

Jimmy M
  • 25
  • 5
0

Seems you are using outdated version of django.. Simply update django and try again.. Following command will update your django version..

pip install --upgrade django

Gihan Gamage
  • 2,944
  • 19
  • 27
0
from django.contrib import admin
from django.urls import path


urlpatterns = [
    path('admin/', admin.site.urls),
]
Mayunga6
  • 11
  • 1