Я новый в 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' на два оператора? Does '{% if selected_choice == 'Yellow'%}' решить проблему? – soon