Дошел до последней главы 4го урока http://djbook.ru/rel1.7/intro/tutorial04.html#amend-views *Получаю ошибку такого рода:
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['poll/(?P<question_id>\d+)/vote/$']
<br />
Интересно, что с самописными функциями index, detail, results все работает.<br />
*detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Votes detail page</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
{% if error_message %} <p><strong>{{ error_message }}</strong></p> {% endif %}
<form action="{% url 'poll:vote' question.id %}" method="post">
{% csrf_token %}
{% for choise in question.choise_set.all %}
<input type="radio" name="choise" id="choise{{ forloop.counter }}" value="{{ choise.id }}"/>
<label for="choise{{ forloop.counter }}">{{ choise.choise_text }}</label> <br />
{% endfor %}
<input type="submit" value="Vote">
</form>
</body>
</html>
views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.core.urlresolvers import reverse
from django.views import generic
from poll.models import Choise, Questions
"""
def index(request):
return HttpResponse("Привет, мир. Эта тестовая версия приложения")
def detail(request, question_id):
question = get_object_or_404(Questions, pk=question_id)
return render(request, 'detail.html', {'question': question})
def results(request, question_id):
question = get_object_or_404(Questions, pk=question_id)
return render(request, 'results.html', {'question': question})
"""
class IndexView(generic.ListView):
template_name = 'index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Questions.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Questions
template_name = 'detail.html'
class ResultsView(generic.DetailView):
model = Questions
template_name = 'results.html'
def vote(request, question_id):
p = get_object_or_404(Questions, pk=question_id)
try:
selected_choise = p.choise_set.get(pk=request.POST['choise'])
except(KeyError, Choise.DoesNotExist):
return render(request, 'detail.html', {
'question': p,
'error_message': 'Вы не выбрали вариант!',
})
else:
selected_choise.votes += 1
selected_choise.save()
return HttpResponseRedirect(reverse('poll:results', args=(p.id,)))
poll/urls.py
from django.conf.urls import patterns, url
from poll import views
urlpatterns = patterns('',
# /poll/
url(r'^$', views.IndexView.as_view(), name='index'),
# /poll/5
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
# /poll/5/results
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
# /poll/5/vote
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
)
P.S. В моем приложении названия polls = poll, choice = choise, Question = Questions
Updated 12 Jan. 2015, 20:19 by MetalHead.