Django noob:AttributeError:'module'对象没有属性'index'(官方教程)

时间:2017-09-04 11:42:28

标签: python django

回溯:

Sahands-MBP-2:mysite sahandz$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function wrapper at 0x10e045c08>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Library/Python/2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Library/Python/2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Library/Python/2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/sahandz/Documents/Programming/Learning Web development/Django/mysite/mysite/urls.py", line 20, in <module>
    url(r'^polls/', include('polls.urls')),
  File "/Library/Python/2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/sahandz/Documents/Programming/Learning Web development/Django/mysite/polls/urls.py", line 6, in <module>
    url(r'^$', views.index, name='index'),
AttributeError: 'module' object has no attribute 'index'

mysite的/ mysite的/ urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

mysite的/轮询/ urls.py:

from django.conf.urls import url

from . import views

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

mysite的/轮询/ views.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

from django.http import HttpResponse

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

# Create your views here.

文件层次结构:

mysite

- manage.py

- mysite
--- __init__.py
--- settings.py
--- urls.py
--- wsgi.py

- polls
--- __init__.py
--- admin.py
--- apps.py
--- migrations
----- __init__.py
--- models.py
--- tests.py
--- urls.py
--- views.py

错误令我感到困惑,因为我的文件是官方教程的COPIES。我已经看到其他人有这个问题,但为他们提供的解决方案似乎并没有解决我的问题,例如将url(r'^$', views.index, name='index'),更改为url(r'^$', 'views.index', name='index'),只会导致另一个错误(第二个参数不应该是字符串)。

2 个答案:

答案 0 :(得分:1)

试试这个:

from django.conf.urls import url

from polls.views import index

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

我希望您在自己的设置中包含民意调查:

INSTALLED_APPS = [
    # ... default values
    'polls',
]

答案 1 :(得分:-1)

您需要将其更改为:

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

这告诉Django查看'views'文件以及名为'index'的视图