Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Jinja 2中使用条件if语句?_Python_Django_Django Templates_Jinja2 - Fatal编程技术网

Python 如何在Jinja 2中使用条件if语句?

Python 如何在Jinja 2中使用条件if语句?,python,django,django-templates,jinja2,Python,Django,Django Templates,Jinja2,所以我是Django的新手,需要一些帮助。 我使用for循环来显示数据库中的列表。但是我想添加一个if语句,这样,如果用户输入与我的数据库项匹配,那么只显示它。看一看: {%for inc in all_items%} <ul> {#I want to add an if statement here, if user input == inc_element#} <li><p>{{in

所以我是Django的新手,需要一些帮助。 我使用for循环来显示数据库中的列表。但是我想添加一个if语句,这样,如果用户输入与我的数据库项匹配,那么只显示它。看一看:

{%for inc in all_items%}
    <ul>                 
        {#I want to add an if statement here, if user input == inc_element#}
        <li><p>{{inc.item_name}}<p></li>
    </ul>
    <hr>
{%endfor%}
{%for inc在所有\u项目中%}
    {#我想在这里添加一个if语句,if user input==inc#u element}
  • {{inc.item_name}}

{%endfor%}

我知道我必须使用HTML论坛来获取用户输入。但在if语句中如何匹配它呢。非常感谢您的帮助。

一般条件语法如下:

{% if some_variable == some_value %}
    {{ do_something }}
{% endif %}

文档还有一些。注意:混乱是一个变量

下面是Python语言中的“for循环”和“if语句”与HTML文件中的Jinja一起使用的代码语法:

{% for mess in get_flashed_messages() %}
    {%if mess == "Your Dog's breed is rufus"%}
      {{mess}} 
    {% else %}
      {{"Don't you have any other breed?"}}
      {% endif %}      
    {% endfor %}
{% for mess in get_flashed_messages() %}
    {%if mess == "Your Dog's breed is rufus"%}
     <div class="alert alert-warning alert-dismissible fade show" role="alert">
      {{mess}} 
    {% else %}
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
      {{"Don't you have any other breed?"}}
      {% endif %}      
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div> 
    </div>
    {% endfor %}
下面是在HTML文件中使用Jinja和引导的代码:

{% for mess in get_flashed_messages() %}
    {%if mess == "Your Dog's breed is rufus"%}
      {{mess}} 
    {% else %}
      {{"Don't you have any other breed?"}}
      {% endif %}      
    {% endfor %}
{% for mess in get_flashed_messages() %}
    {%if mess == "Your Dog's breed is rufus"%}
     <div class="alert alert-warning alert-dismissible fade show" role="alert">
      {{mess}} 
    {% else %}
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
      {{"Don't you have any other breed?"}}
      {% endif %}      
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div> 
    </div>
    {% endfor %}
{%for get_flashed_messages()中的混乱%}
{%if mess==“你的狗的品种是鲁弗斯”%}
{{mess}}
{%else%}
{{“你没有别的品种吗?”}
{%endif%}
&时代;
{%endfor%}

您知道如何将用户输入存储到jinja中吗variable@ArjunKashyap只需将用户输入值传递到模板上下文,就像传递其他变量一样。@ArjunKashyap这篇文章刚刚出现在我的屏幕上。它被碰撞了,因为它没有标记为已应答。我觉得这个答案对你有帮助,所以请把它标记为已接受,这样这个帖子就不会再出现,其他没有答案的帖子也会出现。如果你的问题已经得到回答,请接受答案。其他任何事都是不礼貌的。谢谢