hkhr hkhr - 3 months ago 45
Python Question

error in import django urls

I am following the official django tutorial https://docs.djangoproject.com/en/1.10/intro/tutorial04/

i am using django 1.9.8
till tutorial 3 i did not have any problem.

I am now getting following error

File "C:\Users\hk\Documents\mysite\polls\views.py", line 4, in <module>
from django.urls import reverse
ImportError: No module named urls


following is the the place where i am importing urls -->
/polls/views.py

polls/views.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse

from .models import Choice, Question
# ...


mysite/urls.py

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

admin.autodiscover()

urlpatterns=[
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]


mysite/mysite/urls.py

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

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]

Answer

That tutorial requires Django 1.10 while you seem to be using an older version. In this case, you should import reverse as follows:

from django.core.urlresolvers import reverse

To upgrade your Django version, use the following command (in your command line, not in the Python shell):

pip install --upgrade django