gpt4 book ai didi

python - 属性错误: 'module' object has no attribute 'index'

转载 作者:太空宇宙 更新时间:2023-11-03 18:19:24 25 4
gpt4 key购买 nike

我目前正在浏览 Django 文档站点上提供的官方教程,并且遇到了 AttributeError。这是我正在使用的代码:

“polls”是我的应用程序的名称。

View .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')
)

url.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')
)

最佳答案

views.py 应该位于 polls/views.py

from django.http import HttpResponse

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

关于python - 属性错误: 'module' object has no attribute 'index' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24420151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com