NoReverseMatch - Django 1.7 初学者教程 [英] NoReverseMatch - Django 1.7 Beginners tutorial
问题描述
我正在关注 Django 1.7.1 中的初学者教程,但出现此错误
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 emplatespolldetail.html, error at line 12`
经过一些研究,我发现有人问了类似的问题,有人建议他们应该从一般 url 中删除现金 $
,因为 urlloader 只接受空字符串,而这不会给我错误 No Reverse Match,它把其他一切都搞砸了,每当我尝试访问任何其他 url 时,它会将我重定向到主 url,而无需删除现金 $
我可以完美地继续访问这些 url.那么我做错了什么?
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?
这里是项目网址:
urlpatterns = patterns('',
url(r'^poll/', include('poll.urls', namespace="poll")),
url(r'^admin/', include(admin.site.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'),
)
和意见:
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,)))
另外我想这可能与操作 {%URL%}
和方法 post
的传递方式有关,所以这里是代码行错误中指定的模板文件 <form action="{% url 'poll:vote' question.id %}" method="post">
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
推荐答案
urls.py
中的 URL 名称是 votes
并且您正在搜索 poll:投票
,修复它:
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屋!