2014-12-25 105 views
3

我下面在Django 1.7.1的教程begginers並正在此錯誤NoReverseMatch - Django的1.7初學者教程

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

經過一些研究,我發現人問類似的問題,有人建議他們應該刪除現金$從一般的網址,因爲urlloader只是空的字符串,而這不會給我的錯誤沒有反向匹配,它會擾亂其他一切,每當我嘗試到達任何其他URL它重定向到我的主要網址,而沒有刪除現金$我可以完全進入這些網址。那麼,我做錯了什麼?

下面是該項目URLS:

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">

請讓我知道如果你需要任何東西,並在此先感謝

回答

4

urls.py URL名稱是votes和您正在搜索poll:vote,解決它:

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

哦,謝謝,這有幫助。但是,爲什麼它不被認爲是關鍵字錯誤或「無法找到此網址」的錯誤,我的意思是爲什麼反向錯誤?當我從url(r'^ $',views.IndexView.as_view(),name ='index'), 中刪除現金時會發生什麼?爲什麼它使我保持在同一頁面中,並且不會給我反向錯誤? –