2017-10-06 3 views
1

私は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> 

答えて

0

チュートリアルのようにQuestionではなく、モデル名をQuestionsに変更しました。詳細ビューでは、テンプレート内のモデルインスタンスに対して小文字のモデル名が使用されます。あなたの場合、これはquestionsです。questionを使用するテンプレートのチュートリアルと一致しません。

class ResultsView(generic.DetailView): 
    model = Questions 
    context_object_name = 'question' 

それとも、代わりにquestionquestionsを使用するようにテンプレートを変更する必要があります:あなたはどちらかがあなたのテンプレートが機能するように'question'context_object_nameを設定する必要があることを意味

<h1>{{ questions.question_text }}</h1> 

<ul> 
{% for choice in questions.choice_set.all %} 
    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ 
    choice.votes|pluralize }}</li> 
{% endfor %} 
</ul> 

<a href="{% url 'polls:results' questions.id %}">Vote again?</a> 

Questionにモデルの名前を変更することが良いでしょう - ジャンゴでの標準ではなく、モデル名の複数の単数を使用することです。これを行う場合は、makemigrationsを実行してからmigrateを実行してモデルの名前を変更する必要があります。問題が発生する場合は、マイグレーションとデータベースを削除して、新しいデータベースにmakemigrationsmigrateを実行する方が簡単かもしれません。

+0

ありがとうございました。初めてPythonとDjangoを学ぶときに、私は正しい標準に変更します、あなたの助言をいただきありがとうございます。 –

関連する問題

 関連する問題