我目前正在浏览Django文档站点上提供的官方教程,并遇到过AttributeError。这是我正在使用的代码:
'polls'是我的申请名称。
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
\轮询\ 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)),
)
错误:
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
我的代码似乎有什么问题?
编辑1:
我替换了
行from polls import views
带
from polls.views import index
但是,现在我得到一个NameError:
Exception Type: NameError
Exception Value:
name 'views' is not defined
在我/polls/urls.py源代码的第6行:
from django.conf.urls import patterns, url
from polls.views import index
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
答案 0 :(得分:1)
views.py
应位于polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")