如果条件在 html 中

sun*_*ysm 1 html css python if-statement

我是新来的django。在这里,我尝试使用django. 我想显示'你输入正确'

if selected_choice='Yellow'
Run Code Online (Sandbox Code Playgroud)

这是我的代码

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)
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

rka*_*kam 6

如果 selected_choice 是字符串值:

'==' 前后的空格很重要;这通常被错过。

   {% if selected_choice == 'Yellow' %}
    <p> you entered correct </p>
   {% endif %}
Run Code Online (Sandbox Code Playgroud)

您也可以尝试:

   {% ifequal selected_choice 'Yellow' %} 
    <p> you entered correct </p>
   {% endifequal %}
Run Code Online (Sandbox Code Playgroud)