我查看了其他人在类似情况下所做的事情,我在 INSTALLED_APPS 中检查了我的 settings.py 文件以进行民意调查,甚至添加了“详细信息”(这破坏了事情)。我还检查了我所有的__init__.py
文件,它们似乎都在它们所属的地方。当我放一个__init__.py
文件放入 /Users/apisgirl/mysite/polls/templates/polls (这是本教程建议我在此阶段执行的操作,也是我正在处理模板的地方)时,它会破坏一切。所以这是不行的。
目前情况是这样的:
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,))),