当前位置:  开发笔记 > 编程语言 > 正文

NoReverseMatch Django教程1.8第4章

如何解决《NoReverseMatchDjango教程1.8第4章》经验,为你挑选了1个好方法。

所以我也在Django教程第4章的最后得到noReverseMatch错误.但在我的案例中,其他答案似乎都没有帮助.我无法理解为什么教程会提供一些不起作用的东西.

我自己键入所有内容,因此某处肯定会有拼写错误,但我尝试用第4章中的复制/粘贴交换所有代码,但我仍然遇到同样的错误:

这是错误:

NoReverseMatch at /polls/
Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P[0-9]+)/$']

Error during template rendering

In template /home/MyName/tutorial/mysite/polls/templates/polls/index.html, error at line 4
Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P[0-9]+)/$']

这是urls.py:

    from django.conf.urls import url
    from . import views

    urlpatterns = [
        url(r'^$', views.IndexView.as_view(), name='index'),
        url(r'^(?P[0-9]+)/$', views.DetailView.as_view(), name='detail'),
        url(r'^(?P[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
        url(r'^(?P[0-9]+)/vote/$', views.vote, name='vote')
    ]

这是views.py:

    from django.shortcuts import render, get_object_or_404
    from django.http import HttpResponseRedirect
    from django.core.urlresolvers import reverse
    from django.views import generic

    from .models import Question, Choice

    class IndexView(generic.ListView):
        template_name = 'polls/index.html'
        context_object_name = 'latest_question_list'

        def get_queryset(self):
            return Question.objects.order_by('-pub_date')[:5]

    class DetailView(generic.DetailView):
        model = Question
        template_name = 'polls/detail.html'

    class ResultsView(generic.DetailView):
        model = Question
        template_name = 'polls/results.html'

    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': "You didn't select a choice",})
        else:
            selected_choice.votes += 1
            selected_choice.save()
            return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

这是index.html:

    {% if latest_question_list %}
        
    {% else %}
        

No polls are available.

{% endif %}

Geo Jacob.. 5

将此更改 {% url 'polls:detail' question_id %}{% url 'polls:detail' question.id %}



1> Geo Jacob..:

将此更改 {% url 'polls:detail' question_id %}{% url 'polls:detail' question.id %}

推荐阅读
殉情放开那只小兔子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有