Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ImportError: No module named urls" while following Django Tutorial

Tags:

python

django

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

I am using Django 1.9.8 and I am now getting the following error in /polls/views.py:

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

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')),
]
like image 622
Lohith Avatar asked Aug 14 '16 09:08

Lohith


2 Answers

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
like image 151
Selcuk Avatar answered Nov 14 '22 23:11

Selcuk


Latest tutorial (https://docs.djangoproject.com/en/2.0/intro/tutorial01/) uses Django 2.0, which supports Python 3.4 and later

Since I ran into this same issue I had to upgrade by python as well as django

sudo apt-get -y install python3-pip
pip3 install --upgrade django

to see your django version run following command -

python3 -c "import django; print(django.get_version())"

for me it says 2.0.1

To check python version run -

python3 --version

For me it is Python 3.5.2

like image 43
Aniket Thakur Avatar answered Nov 14 '22 21:11

Aniket Thakur