我正在遵循 Django 1.7.1 中的初学者教程并收到此错误
Reverse for 'vote' with arguments '(5,)' and keyword arguments '{}' not found. 0 pattern(s) tried: [] `poll\templates\poll\detail.html, error at line 12`
经过一番研究后,我发现有人问类似的问题,有人建议他们应该取出现金$
来自一般网址,因为网址加载器只接受空字符串,虽然这不会给我错误“无反向匹配”,但它会搞乱其他所有内容,每当我尝试访问任何其他网址时,它都会将我重定向到主网址,而无需取出现金$
我可以完美地继续访问这些网址。那么我做错了什么?
这是项目网址:
urlpatterns = patterns('',
url(r'^poll/', include('poll.urls', namespace="poll")),
url(r'^admin/', include(admin.site.urls)),
)
以及应用程序 URL:
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">
如果您还需要任何其他信息,请告诉我,并提前致谢