我是django
的新手。在這裏,我嘗試使用django
來做投票應用程序。 我想顯示 '您輸入正確'如果html裏面的條件
if selected_choice='Yellow'
這裏是我的代碼
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
context_dict={}
context_dict['selected_choice']=selected_choice
context_dict['question']=question
return render(request, 'polls/result.html', context_dict)
HTML文件
<h1>{{ question.question_text }}</h1>
{% if selected_choice %}
{% if 'Yellow' %}
<p> you entered correct </p>
{% endif %}
{% endif %}
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
如果在HTML文件中的條件沒有正常工作下。 – sunnysm
如果html內部條件更好,稱爲模板:) – aliasm2k
爲什麼你將'if'語句分成兩個語句?是否{{%if selected_choice =='Yellow'%}'解決了問題? – soon