私はDjangoチュートリアルのジェネリックビューのセクションを自分のウェブサイトから開始しました。 。私はこの質問が以前に尋ねられたことを知っていますが、これらの答えは私の問題を解決しませんでした。私はエラーを見ることができますか?コードを整理するためにインポートを削除しました。
urls.py:
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(),
name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
views.py:
class IndexView(generic.ListView):
template_name = 'polls/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 = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Questions
template_name = 'polls/results.html'
def vote(request, question_id):
question = get_object_or_404(Questions, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=
(question.id,)))
detail.html:
<h1>{{ object.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question_id=object.id %}" method="post">
{% csrf_token %}
{% for choice in object.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>
results.html:
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{
choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:results' question.id %}">Vote again?</a>
ありがとうございました。初めてPythonとDjangoを学ぶときに、私は正しい標準に変更します、あなたの助言をいただきありがとうございます。 –