【发布时间】:2015-12-21 09:02:03
【问题描述】:
我是django 的新手。在这里,我尝试使用 django 做一个投票应用程序。
我想显示'你输入正确'
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文件中的条件不能正常工作。
-
这个 if html 中的条件最好称为模板 :)
-
您为什么将您的
if语句拆分为两个语句?{% if selected_choice == 'Yellow' %}解决问题了吗? -
@rkatkam 的正确答案如下 - 请参阅文档 - docs.djangoproject.com/en/1.6/ref/templates/builtins/#operator
标签: python html css if-statement