6

过去几天我一直在尝试学习 Django,但最近我偶然发现了一个我似乎无法解决的问题。在完成了 Django 自己编写的第一个应用程序的教程后,我决定再看一遍。直到现在,我才会更换所有东西以适应我正在构建的原始应用程序的要求。

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

AttributeError at /lru/
'module' object has no attribute 'index'

追溯:

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'

我的代码:

视图.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')
)

我的网站/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")),
)

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

mysite/
    lru
    templates
    polls
    manage.py
    mysite

lru/
    templates
    urls.py
    admin.py
    __init__.py
    models.py
    tests.py
    views.py

这很奇怪,因为我所做的一切都与我在“民意调查”示例教程中所做的完全一样。只是换个名字。url(r'^lru/', include('lru.urls', namespace="lru")),当我在 mysite/urls.py 中注释掉时,http://localhost:8000/polls/工作正常,但我似乎无法让 /lru 工作。

这真的让我很生气,所以任何形式的帮助都会不胜感激!

编辑:添加了完整的回溯

4

5 回答 5

5

将 urls.py 模块导入您的视图。像这样;

from django.http import HttpResponse
from . import urls

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")
于 2017-06-24T17:08:43.380 回答
3

要么这样做:

from lru.views import *

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

或者

from lru import views

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

我希望这有帮助。

于 2013-07-26T11:40:03.903 回答
1

的第二个参数url()应该是字符串,我会将其更改lru/urls.py为:

from django.conf.urls import patterns, url

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

希望能帮助到你!

于 2013-07-26T11:45:37.837 回答
0

这就是 Django URL 的工作方式:

  1. 来自mysite/mysite/urlsincludes的 urlmysite/polls/urls表明在 mysite 项目中创建了民意调查;
  2. mysite/polls/urls文件告诉我们,无论何时访问 url “polls/”,都应该调用该函数,当您键入该行时,它会index()从该函数中声明:mysite/polls/views
    urlpatterns = [    
        path('', views.index, name = 'index'),    
    ]
  1. 要记住的一件非常重要的事情是在运行服务器之前保存每个编辑过的文件,以便文件得到更新。您还可以保持服务器运行并保存文件。
于 2021-11-14T13:30:53.690 回答
0

我刚刚发现 Sublime 文本正在混合......和制表符进行缩进,我猜这取决于复制/粘贴源。

于 2021-03-15T19:22:20.243 回答