django教程 - 投票时的通用视图404

时间:2011-12-26 18:38:13

标签: python django

我得到了一个教程的实例,几乎在共享主机(小奇迹)上工作。在投票之前一切正常。我很难过为什么要投票给:

/轮询/ 1 /轮询/ 1 /表决/

而不是/ polls / 1 / vote。

当我破解[site] /民意调查/ 1 /投票的网址时,我确实通过“你没有选择一个选项来重新显示民意调查问题。”

这是整个404(我希望,显示得足以让我不需要包含我的urls.py,但我很乐意,如果有帮助的话):

Using the URLconf defined in urls, Django tried these URL patterns, in this order:
^polls/ ^$
^polls/ ^(?P<pk>\d+)/$
^polls/ ^(?P<pk>\d+)/results/$ [name='poll_results']
^polls/ ^(?P<poll_id>\d+)/vote/$
^admin/
The current URL, polls/1/polls/1/vote/, didn't match any of these.

这是我的views.py:

from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from polls.models import Choice, 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_to_response('polls/detail.html', {
            'poll': p,
            'error_message': "You didn't select a choice.",
        }, context_instance=RequestContext(request))
    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.views.results', args=(p.id,)))

感谢任何人提供的帮助,如果我没有发布足够的代码,请告诉我,我很乐意发布更多内容。

0 个答案:

没有答案