本文介绍了Django:'module'对象没有属性'index'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天我一直在尝试学习Django,但最近我偶然发现了一个我似乎无法解决的问题。在完成Django自己的教程第一个应用程序之后,我决定再次通过它。只有现在我将替​​换所有的东西,以适应我正在建立的原始应用程序的要求。



所以,一切顺利,直到我到第3部分。当我尝试加载 http:// localhost:8000 / lru / 我收到以下错误消息:

  / lru / 
'module'对象的AttributeError没有属性'index'

追溯:

 内部服务器错误:/favicon.ico 
追溯(最近呼叫最后):
文件/Library/Python/2.7/site-packages/django/core/handlers/base.py,第92行,get_response
response = middleware_method(request)
文件/ Library /如果(不是urlresolvers.is_valid_path(request.path_info,urlconf)和
文件/ Library / Python,那么在第一行中,Python / 2.7 / site-packages / django / middleware / common.py /2.7/site-packages/django/core/urlresolvers.py,第551行,is_valid_path
resolve(path,urlconf)
文件/ Library / Py thon / 2.7 / site-packages / django / core / urlresolvers.py,第440行,解析
return get_resolver(urlconf).resolve(path)
文件/Library/Python/2.7/site -packages / django / core / urlresolvers.py,第319行,解析
for self.url_patterns中的模式:
文件/Library/Python/2.7/site-packages/django/core/urlresolvers .py,第347行,url_patterns
patterns = getattr(self.urlconf_module,urlpatterns,self.urlconf_module)
文件/Library/Python/2.7/site-packages/django/core/ urlresolvers.py,第342行,urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
文件/Library/Python/2.7/site-packages/django/utils/importlib.py,第35行,import_module
__import __(name)
文件/ Users / oyvindhellenes / Desktop / Sommerjobb 2013 / mysite / mysite / urls.py,第10行,< module>
url(r'^ lru /',include('lru.urls',namespace =lru)),
文件/Library/Python/2.7/site-packages/django/conf/ urls / __ init__.py,第25行,include
urlconf_module = import_module(urlconf_module)
文件/Library/Python/2.7/site-packages/django/utils/importlib.py,第35行,in import_module
__import __(name)
文件/ Users / oyvindhellenes / Desktop / Sommerjobb 2013 / mysite / lru / urls.py,第6行在< module>
url(r'^ $',views.index,name ='index')
AttributeError:'module'对象没有属性'index'
/ pre>

我的代码:



views.py



pre $ from django.http import HttpResponse

def index(request):
return HttpResponse(Hello,world。索引)

lru / urls.py



来自django.conf.urls导入模式的

 ,来自lru导入视图的url 



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

mysite / 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)),
ur l(r'^ lru /',include('lru.urls',namespace =lru)),

我的文件夹结构如下所示:

  mysite / 
lru
模板
民意调查
manage.py
mysite

lru /
模板
urls.py
admin.py
__init__.py
models.py
tests.py
views.py

这很奇怪,因为我已经完成了一切,就像我在民意测验例子中所做的一样。只是替换名字。当我在mysite / urls.py中注释出 url(r'^ lru /',include('lru.urls',namespace =lru)), http:// localhost:8000 / polls / 工作正常,但我似乎无法让/ lru上班。



这真的是杀了我,所以任何形式的帮助将是感激的!



编辑:添加完全追踪

解决方案

可以这样做:

  from lru.views import * 

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

  from lru import views 

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

我希望这有帮助。


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.

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:

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

from django.http import HttpResponse

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

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

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.

This is really killing me so any form of help would be appreciative!

Edit: Added full traceback

解决方案

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.

这篇关于Django:'module'对象没有属性'index'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:13