Python 我应该在django模板中使用if语句吗;我在models.py中使用它

Python 我应该在django模板中使用if语句吗;我在models.py中使用它,python,django,Python,Django,这是我的代码,我会解释我的代码有什么问题。 我有 在我的模特里。 在我的模板中,我使用上述函数 <a href='#' class='replies'> {{comment.comment_count}} replies</a> 但问题是,如果comment_set.count为零,我不希望有任何东西。现在,如果没有注释_set.count,则不会显示回复 我可能可以使用django模板语言来实现这一点,并执行以下操作 {%if c

这是我的代码,我会解释我的代码有什么问题。 我有

在我的模特里。 在我的模板中,我使用上述函数

<a href='#' class='replies'>           
      {{comment.comment_count}} replies</a>

但问题是,如果comment_set.count为零,我不希望有任何东西。现在,如果没有注释_set.count,则不会显示回复

我可能可以使用django模板语言来实现这一点,并执行以下操作
{%if comment.comment\u count>0%}
但我不知道这是否是最好的方法。我甚至不确定这是否正确

Do:

{% if comment.comment_count > 0 %}
    <a href='#' class='replies'>{{comment.comment_count}} replies</a>
{% endif %}
{%if comment.comment\u count>0%}
{%endif%}
在这种情况下可以使用逻辑:)

执行以下操作:

{% if comment.comment_count > 0 %}
    <a href='#' class='replies'>{{comment.comment_count}} replies</a>
{% endif %}
{%if comment.comment\u count>0%}
{%endif%}
在这种情况下可以使用逻辑:)

和在模板中

{% if comment.comment_count %}
<a href='#' class='replies'>{{comment.comment_count}} replies</a>
{% endif %}
{%if comment.comment\u count%}
{%endif%}
和在模板中

{% if comment.comment_count %}
<a href='#' class='replies'>{{comment.comment_count}} replies</a>
{% endif %}
{%if comment.comment\u count%}
{%endif%}

是的,但他问使用DTLYS是否可以,但他问使用DTLYS是否可以谢谢你我不知道为什么我没有想到这一点,我想我认为Comemnu set.count仍然存在于0…谢谢你我不知道为什么我没有想到这一点,我想我认为Comemnu set.count仍然存在于0。。。