Chetan
Chetan

Reputation: 51

Import Error While Executing Django Tutorial

I am following Django tutorial given in the following link:-

http://docs.djangoproject.com/en/1.4/intro/tutorial01/

While running server I get many error messages:-

ImportError at /admin/

No module named polls.urls

Request Method:     GET
Request URL:        http://127.0.0.1:8000/admin/
Django Version:     1.4.1
Exception Type:     ImportError
Exception Value:    

No module named polls.urls

Exception Location:     C:\Python27\lib\site-packages\django\utils\importlib.py in import_module, line 35
Python Executable:      C:\Python27\python.exe
Python Version:         2.7.3

Python Path:    

['D:\\chetan\\All_My_Projects\\mysite1',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']

The errors are similar to what has been asked in:-

No module named urls

My question is, do we need to add the project directory,which in my case would be D:\chetan\All_My_Projects\mysite1 to environment variable? Is this causing the problem..

Upvotes: 2

Views: 3630

Answers (4)

tresf
tresf

Reputation: 7922

The tutorial could benefit from using absolute paths, e.g. mysite/mysite/urls.py, etc. to avoid confusion. This is why...

The django tutorial is a bit confusing because it creates a project mysite (which most of us are going to give a different name) and then within that project mysite it also has a site called mysite.

  • One mysite directory is used for starting the server because it contains manage.py
  • The other nested mysite contains the urls.py which maps to the new app (the tutorial calls this app polls).

So although the tutorial clearly explains what the directory structure should look like, if one tries to follow it too quickly it's not immediately obvious that the app polls is supposed to reside side-by-side the site mysite). thus creating mysite/mysite, mysite/polls or perhaps djangotest/djangotest, djangotest/myapp, etc.

When following this tutorial, if one had created the necessary files inside the wrong directory, it won't be immediately obvious what that person had done wrong resulting in the error No module named polls.urls.

Upvotes: 0

Surya Kasturi
Surya Kasturi

Reputation: 4753

This is the whole Django Project ( I suppose, you are on Windows, using Py 27 )

Follow these steps:

  • open cmd, direct it to d:\, Now python c:/python27/lib/site-packages/django/bin/django-admin.py startproject mysite

  • cd mysite and python manage.py startapp polls

Now, cd polls

# polls/views.py
from django.shortcuts import render_to_response
def polls_home(request):
    return render_to_response("polls.html")

# polls/urls.py
from django.conf.urls import patterns, include, url
import views
urlpatterns = patterns ("",
                            url(r'^polls$', views.polls_home),
)
  • Go to d:/mysite/mysite/settings.py and add polls in INSTALLED_APPS

  • very important: open mysite/mysite/urls.py

add url(r'^home$', include('polls.urls') ), to url patterns

Now, runserver and open /home/polls

Upvotes: 1

Chetan
Chetan

Reputation: 51

The Django tutorial executed properly after adding my project in PythonPath.i.e, my project was created in 'D:\chetan\All_My_Projects\mysite1'. All i had to do in Aptana Studio is:-

1.Go to Windows->Preferences->PyDev->Interpreter-Python

2.Add 'D:\chetan\All_My_Projects\mysite1' to the PythonPath.

Thank you all for your help. I would also like to refer to the following link which has emphasized on adding the project to the PythonPath:-

http://www.webmonkey.com/2010/02/Install_Django_and_Build_Your_First_App

Upvotes: 0

Is it possible that you have typed in <your_app>/urls.py:

url(r'^polls/', include(polls.urls))

Instead of this? (the quotes are needed):

url(r'^polls/', include('polls.urls'))

I would check if polls is in INSTALLED_APPS inside <your_app>/settings.py if that doesn't work.

Upvotes: 0

Related Questions