1

I'm currently going through the official tutorial provided on Django's documentation site, and have encountered an AttributeError. Here's the code I'm working with :

'polls' is the name of my application.

views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")

\polls\urls.py

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index')
)

urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    url(r'^blog/', include('Blog.urls')),
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

Error:

AttributeError at /polls

'module' object has no attribute 'index'

Request Method:     GET
Request URL:    http://localhost:8000/polls
Django Version:     1.6.5
Exception Type:     AttributeError
Exception Value:    

'module' object has no attribute 'index'

Exception Location:     C:\Users\manoj\Desktop\Django\mysite\polls\urls.py in <module>, line 6
Python Executable:  F:\Python 2.7\python.exe
Python Version:     2.7.5
Python Path:    

['C:\\Users\\manoj\\Desktop\\Django\\mysite',
 'F:\\Python 2.7\\lib\\site-packages\\pip-1.4.1-py2.7.egg',
 'C:\\Windows\\system32\\python27.zip',
 'F:\\Python 2.7\\DLLs',
 'F:\\Python 2.7\\lib',
 'F:\\Python 2.7\\lib\\plat-win',
 'F:\\Python 2.7\\lib\\lib-tk',
 'F:\\Python 2.7',
 'F:\\Python 2.7\\lib\\site-packages']

Server time:    Thu, 26 Jun 2014 04:44:51 +0530

What seems to be the problem with my code ?


EDIT 1:

I replaced the line

from polls import views

with

from polls.views import index

However, now I get a NameError:

Exception Type:     NameError
Exception Value:    

name 'views' is not defined

in the 6th line of my /polls/urls.py source code:

from django.conf.urls import patterns, url

from polls.views import index

urlpatterns = patterns('',
    url(r'^$', views.index, name='index')
)
2
  • There could be some conflict with the package name. Just try changing from polls import views to from polls.views import index Commented Jun 26, 2014 at 0:13
  • Now I'm getting a NameError. I have edited my original post above to include the details of the error that I encountered. Commented Jun 26, 2014 at 10:10

1 Answer 1

1

views.py should be inside polls/views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.