2015-02-08 60 views
0

我一直在撞牆我整個上午都試圖完成Django 1.7 tutorial04。我已經通過了有關NoReverseMatch錯誤在stackoverflow上所有類似的帖子,我的錯誤是略有不同:Django 1.7.4:Django 1.7錯誤教程04:NoReverseMatch錯誤的反向投票'

Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>\\d+)/vote/$']

看來它窒息我details.html形式action屬性:

<body bgcolor="white"> 
 
    <h1>{{ question.question_text }}</h1> 
 
    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 
 

 
    <form action="{% url 'polls:vote' question_id %}" method="post"> 
 
    {% csrf_token %} 
 
    {% for choice in question.choice_set.all %} 
 
     <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> 
 
     <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br /> 
 
    {% endfor %} 
 
     <input type="submit" value="Vote" /> 
 
    </form> 
 
</body>

我已經在我的主定義的URL投票命名空間,並得到它的工作在我的調查指數頁面 - ul裏面的一個標籤使用{% url 'polls:detail' question.id %},它工作得很好。

我的投票/ urls.py的樣子:

from django.conf.urls import patterns, url 

from polls import views 

urlpatterns = patterns('', 
    url(r'^$',views.index, name='index'), 
    url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'), 
    url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'), 
    url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'), 
) 

最後我投票/ views.py:

from django.shortcuts import render, get_object_or_404 
from django.http import HttpResponse,Http404 
from django.http import HttpResponseRedirect, HttpResponse 

from polls.models import Question,Choice 

# Create your views here. 

def index(request): 
    latest_question_list = Question.objects.order_by('-pub_date')[:5] 
    context = {'latest_question_list': latest_question_list} 
    return render(request, 'polls/index.html', context) 

def detail(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    return render(request, 'polls/detail.html', {'question':question}) 

def results(request, question_id): 
    response = HttpResponse("You're looking at the results of question %s.") 
    return HttpResponse(response % question_id) 

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): 
     # redisplay the question voting form 
     return render(request, 'polls/detail.html', { 
      'question': p, 
      'error_message': "You didn't select a choice.", 
     }) 
    else: 
     selected_choice.votes += 1 
     select_choice.save() 
     # always return response redirect after dealing with POST 
     # to prevent reposts if user hits back button 
     return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) 

錯誤:

NoReverseMatch at /polls/1/ 

Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>\\d+)/vote/$'] 

Request Method:  GET 
Request URL: http://localhost:8000/polls/1/ 
Django Version:  1.7.4 
Exception Type:  NoReverseMatch 
Exception Value:  

Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>\\d+)/vote/$'] 

Exception Location:  /usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 468 
Python Executable: /usr/bin/python 
Python Version:  2.7.9 

任何援助將大大讚賞。謝謝你的閱讀。

回答

2

相反的question_id,嘗試使用模板中question.id,即:

<form action="{% url 'polls:vote' question.id %}" method="post"> 
+0

這樣一個愚蠢的錯誤!我習慣於使用主鍵ID使用_s進行手動sql查詢。非常感謝! – Keith 2015-02-08 16:17:12