NoReverseMatch - Django 1.7初学者教程 [英] NoReverseMatch - Django 1.7 Beginners tutorial

查看:124
本文介绍了NoReverseMatch - Django 1.7初学者教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循Django 1.7.1中的begginers教程,并且收到此错误

 反向投票与参数'(5,)'和关键字参数'{}'未找到。 0模式尝试:[]`poll\templates\poll\detail.html,错误在第12行

经过一番研究,我发现人们提出类似的问题,有人建议他们从普通网址中删除现金 $ ,因为urlloader只是占用空字符串,而这并没有给我错误无反向匹配,它混淆了所有其他的东西,每当我尝试到达任何其他url它重定向到主要的url,而不删除现金 $ 我可以完美地继续这些网址。那么我做错了什么?



这里是项目URLS:

  urlpatterns = patterns('',
url(r'^ poll /',include('poll.urls',namespace =poll)),
url(r' admin /',include(admin.site.urls)),

应用程序URLS:

  from django.conf.urls import patterns,url 

from poll import views

urlpatterns = patterns('',
#eg / poll /
url(r'^ $',views.IndexView.as_view(),name ='index'),
#eg / poll / 5 /
url(r'^(?P< pk> \d +)/ $',views.DetailView.as_view(),name ='detail'),
#eg / poll / 5 / results /
url(r'^(?P< pk> \d +)/ results / $',views.ResultsView.as_view(),name ='results' ),
#eg / poll / 5 / votes /
url(r'^(?P< question_id> \d +)/ votes / $',views.votes,name ='votes' ,

视图:从django.shortcuts导入render,get_object_or_404
从django.http导入HttpResponseRedirect
from django.core

 .urlresolvers import reverse 
from django.views import generic
from poll.models import Question,Choice


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

def get_queryset(self):
返回最后五个发布的问题。
return Question.objects.order_by(' - pup_date')[:5]


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


class ResultsView(generic.DetailView):
model = Question
template_name ='poll / 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,'poll / detail.html',{
'question':p,
'error_message':你没有选择一个选择,
})
else:
selected_choice.votes + = 1
selected_choice.save()
#成功处理
#与POST数据之后,始终返回一个HttpResponseRedirect。如果
#用户点击后退按钮,则可以防止数据发布两次。
return HttpResponseRedirect(reverse('poll:results',args =(p.id,)))

此外,我猜这可能与动作 {%URL%} 和方法 post 正在传递,所以这里是错误< form action ={%url'poll:vote'question.id%}中指定的模板文件的代码行method =post < / code>



如果您需要其他任何信息,请告诉我们,并提前感谢

解决方案

urls.py 中的URL名称为投票,而您正在搜索 poll:vote ,修复它:

 < form action ={%url'poll:votes'question.id%}method =post> 
HERE ^


I am following the begginers tutorial in Django 1.7.1 and am getting this error

Reverse for 'vote' with arguments '(5,)' and keyword arguments '{}' not found. 0 pattern(s) tried: [] `poll\templates\poll\detail.html, error at line 12`

after a bit of research I found people asking similar question and someone suggested that they should remove the cash $ from the general url, because the urlloader just takes the empty string, while this doesn't gives me the error No Reverse Match, it messes everything else up, whenever I try to reach any other url it redirects me to the main url, while without removing the cash $ I can perfectly proceed to these urls. So what is it that I am doing wrong?

Here is the project URLS:

urlpatterns = patterns('',
    url(r'^poll/', include('poll.urls', namespace="poll")),
    url(r'^admin/', include(admin.site.urls)),
)

And the app URLS:

from django.conf.urls import patterns, url

from poll import views

urlpatterns = patterns('',
    #e.g. /poll/
    url(r'^$', views.IndexView.as_view(), name='index'),
    #e.g. /poll/5/
    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    #e.g. /poll/5/results/
    url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
    #e.g. /poll/5/votes/
    url(r'^(?P<question_id>\d+)/votes/$', views.votes, name='votes'),
)

And the views:

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 poll.models import Question, Choice


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

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pup_date')[:5]


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


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


def votes(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):
        # Redisplay the question voting form.
        return render(request, 'poll/detail.html', {
            'question': 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('poll:results', args=(p.id,)))

Also I guess it might be sth related to how the action {%URL%} and the method post are being passed, so here is the code line from the template file specified in the error <form action="{% url 'poll:vote' question.id %}" method="post">

Please let me know if you need anything else, and thanks in advance

解决方案

The URL name in urls.py is votes and you are searching for poll:vote, fix it:

<form action="{% url 'poll:votes' question.id %}" method="post">
                          HERE ^

这篇关于NoReverseMatch - Django 1.7初学者教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆