Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django项目教程4中,我需要渲染(请求…)还是HttpResponseRedirect(反向…)_Python_Django - Fatal编程技术网

Python 在Django项目教程4中,我需要渲染(请求…)还是HttpResponseRedirect(反向…)

Python 在Django项目教程4中,我需要渲染(请求…)还是HttpResponseRedirect(反向…),python,django,Python,Django,在Django项目教程第4部分中,我遇到以下错误: NoReverseMatch at/polls/1/results/ 对于参数为“(“”,)”和关键字参数为“{}”的“详细信息”,则相反 没有找到。已尝试1个模式:[u'polls/(?P\d+/$”] 您的结果视图有输入错误: return render(request, 'polls/results.html', {'guestion': question})

在Django项目教程第4部分中,我遇到以下错误:

NoReverseMatch at/polls/1/results/ 对于参数为“(“”,)”和关键字参数为“{}”的“详细信息”,则相反 没有找到。已尝试1个模式:[u'polls/(?P\d+/$”]


您的
结果
视图有输入错误:

return render(request, 'polls/results.html', {'guestion': question})
                                               ^ g instead of q

因此,您的模板没有
question
值,因此反向URL解析失败-您可以看到错误消息说它正在传递
id
的空白值,而不是
question.id

django的版本是什么?django版本是1.7.3您的投票()函数还有没有if条件的else。在哪一行?你明白了吗error@ancho这是我的另一张照片。谢谢你的回答。但主要错误是一样的:/polls/1/results/真的吗?你重启服务器了吗?请显示完整的回溯。
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<question_id>\d+)/results/$',views.results, name='results'),
    url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader
from polls.models import Question, Choice
from django.http import Http404
from django.core.urlresolvers import reverse


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

def detail(request, question_id):
    try:
        question = Question.objects.get(pk = question_id)
    except Question.DoesNotExist:
        raise Http404("Question ne postoji")
    return render(request, 'polls/detail.html', {'question': question})

def results(request, question_id):
    question = get_object_or_404(Question, pk = question_id)
    return render(request, 'polls/results.html', {'guestion': question})


def vote(request, question_id):
    p = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice  = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/detail.html', {
            'question': p,
            'error_message': "Did not choose answer.",
         })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
return render(request, 'polls/results.html', {'guestion': question})
                                               ^ g instead of q