Python Django:“模块”对象没有属性“索引”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17880146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django: 'module' object has no attribute 'index'
提问by ?yvind Hellenes
I've been trying to learn Django for the past few days, but recently I've stumbled upon a problem I can't seem to fix. After finishing Django's own tutorial on writing your first app I decided to go through it again. Only now I would replace everything to fit the requirements of the original app I was building.
过去几天我一直在尝试学习 Django,但最近我偶然发现了一个我似乎无法解决的问题。在完成了 Django 自己的关于编写第一个应用程序的教程后,我决定再次阅读它。直到现在,我才会替换所有内容以满足我正在构建的原始应用程序的要求。
So, everything went well until I got to part 3. When I try to load http://localhost:8000/lru/
I get the following error message:
所以,一切都很顺利,直到我到达第 3 部分。当我尝试加载时,http://localhost:8000/lru/
我收到以下错误消息:
AttributeError at /lru/
'module' object has no attribute 'index'
Traceback:
追溯:
Internal Server Error: /favicon.ico
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 92, in get_response
response = middleware_method(request)
File "/Library/Python/2.7/site-packages/django/middleware/common.py", line 69, in process_request
if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 551, in is_valid_path
resolve(path, urlconf)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 440, in resolve
return get_resolver(urlconf).resolve(path)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 319, in resolve
for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 347, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/oyvindhellenes/Desktop/Sommerjobb 2013/mysite/mysite/urls.py", line 10, in <module>
url(r'^lru/', include('lru.urls', namespace="lru")),
File "/Library/Python/2.7/site-packages/django/conf/urls/__init__.py", line 25, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/oyvindhellenes/Desktop/Sommerjobb 2013/mysite/lru/urls.py", line 6, in <module>
url(r'^$', views.index, name='index')
AttributeError: 'module' object has no attribute 'index'
My code:
我的代码:
views.py
视图.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
lru/urls.py
lru/urls.py
from django.conf.urls import patterns, url
from lru import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
mysite/urls.py
我的网站/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'^lru/', include('lru.urls', namespace="lru")),
)
My folder structure looks like this:
我的文件夹结构如下所示:
mysite/
lru
templates
polls
manage.py
mysite
lru/
templates
urls.py
admin.py
__init__.py
models.py
tests.py
views.py
It's strange because I've done everything exactly as I did in the "polls" example turtorial. Just replacing the names. When I comment out url(r'^lru/', include('lru.urls', namespace="lru")),
in mysite/urls.py, then http://localhost:8000/polls/
works fine, but I just can't seem to get /lru to work.
这很奇怪,因为我所做的一切都与我在“民意调查”示例教程中所做的完全一样。只是换个名字。当我url(r'^lru/', include('lru.urls', namespace="lru")),
在 mysite/urls.py 中注释掉时,http://localhost:8000/polls/
工作正常,但我似乎无法让 /lru 工作。
This is really killing me so any form of help would be appreciative!
这真的让我很伤心,所以任何形式的帮助都会很感激!
Edit: Added full traceback
编辑:添加了完整的回溯
采纳答案by Manuj Rastogi
Either do this :
要么这样做:
from lru.views import *
urlpatterns = patterns(
'',
url(r'^$', index, name='index')
)
or
或者
from lru import views
urlpatterns = patterns(
'',
url(r'^$', 'views.index', name='index')
)
I hope this helps.
我希望这有帮助。
回答by Hieu Nguyen
The second argument of url()
should be string, anway I would change the lru/urls.py
to:
的第二个参数url()
应该是字符串,我会将其更改lru/urls.py
为:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'lru.views.index', name='lru-index')
)
Hope it helps!
希望能帮助到你!
回答by Collins Ushi
Import the urls.py module into your view. like this;
将 urls.py 模块导入您的视图。像这样;
from django.http import HttpResponse
from . import urls
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")