python - if 里面的条件

标签 python html css if-statement

我是 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>

最佳答案

如果 selected_choice 是一个字符串值:

'=='前后的空格很重要;这通常会被遗漏。

   {% if selected_choice == 'Yellow' %}
    <p> you entered correct </p>
   {% endif %}

你也可以试试:

   {% ifequal selected_choice 'Yellow' %} 
    <p> you entered correct </p>
   {% endifequal %}

关于python - if 里面的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34392045/

相关文章:

python - 如何删除 Django 中的 session 变量?

python - 扩展 get_object_or_404 以使用 select_related

python - 取下一个组值并将其分配给当前组元素

javascript - 让一个 div 有一个最顶层的位置?

html - 如何使用 css 创建像这张图片这样的 div

javascript - 是否可以提取数据 :image of canvas element?

css - 从关键帧中间动画过渡回来

jquery - 中间切换线没有正确消失

jquery - 单击两次时添加事件类而不删除

python - 将 Python 3 打开(编码 ="utf-8")向后移植到 Python 2