Django Tutorial的第3部分 - 'module'对象没有属性'detail'

时间:2013-06-07 22:26:46

标签: python django

我已经查看了其他人在类似情况下所做的事情,我在INSTALLED_APPS中检查了我的settings.py文件进行民意调查,甚至添加了“详细信息”(这打破了事情)。我还检查了所有__init__.py个文件,它们似乎都属于它们所属的位置。当我在/ Users / apisgirl / mysite / polls / templates / polls中放入一个__init__.py文件时(这是我在这个阶段所做的教程,也就是我正在处理模板的地方)它会破坏一切。所以这是不行的。

目前这是破碎的方式:

Environment:


Request Method: GET
Request URL: http://localhost:8000/polls/

Django Version: 1.5.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'polls')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/apisgirl/mysite/mysite/urls.py" in <module>
  19.     url(r'^polls/', include('polls.urls')),
File "/Library/Python/2.7/site-packages/django/conf/urls/__init__.py" in include
  25.         urlconf_module = import_module(urlconf_module)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/apisgirl/mysite/polls/urls.py" in <module>
  10.                        url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),

Exception Type: AttributeError at /polls/
Exception Value: 'module' object has no attribute 'detail'

我也在使用最新版本的osx(所有更新和什么不是),所以我认为这是我自己的错,我只是在这里错过了一些部分。

我想不出我做过的任何其他事情,我保证检查了settings.py,我的所有urls.py文件,以及我可能错过的任何我的views.py文件。我自然会多次阅读教程,在那里寻找答案。我只是没有得到它。

编辑:这与mysite / polls / urls.py中的这一行有关吗?我已经检查了,这就是教程中的样子。这是一个版本问题吗?

url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),

根据请求,这是我的polls / views.py文件,它有一些第4部分的代码,因为我开始寻找答案:

from django.http import HttpResponse
from django.http import HttpResponse
from django.template import Context, loader

from polls.models import Poll

def index(request):
    latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = Context({
                      'latest_poll_list': latest_poll_list,
                      })
    return HttpResponse(template.render(context))


def results(request, poll_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    return render(request, 'polls/results.html', {'poll': poll})

def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the poll voting form.
        return render(request, 'polls/detail.html', {
                      'poll': p,
                      'error_message': "You didn't select a choice.",
                      })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,))),

1 个答案:

答案 0 :(得分:4)

错误说

'module' object has no attribute 'detail'

和堆栈跟踪显示它出现在这一行:

url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail')

很明显,解释器试图告诉我们views模块(对应于您的views.py文件)没有属性detail(此处“属性”表示“无论你用点符号访问什么“,那就是:变量,函数,方法......等等。”

确实,您的views.py缺少detail功能。检查教程的第3部分its defintion