简体   繁体   English

如果是,则使用Django模板在HTML内进行其他操作

[英]If, Else inside html using Django templates

I have a Django project that consists of many html pages. 我有一个包含许多HTML页面的Django项目。

I want to add an if else condition inside my html tag to return "None" whenever the time stamp = 0000-00-00 00:00:00 and return the time when it's not. 我想在我的html标记内添加if if条件,以便只要时间戳= 0000-00-00 00:00:00都返回“ None”,否则返回时间。 My code is shown below, I used a tag to get the time in date format. 我的代码如下所示,我使用了一个标记以日期格式获取时间。

<td>{{ table.start_time|date:"Ymd G:i:s"}}</td>

I just used another tag: 我只是用了另一个标签:

<td>{{ table.start_time|date:"Ymd G:i:s"|default:"None"}}</td>

Thanks all!! 谢谢大家!

You can do this with the ifequal template tag. 您可以使用ifequal模板标签执行此操作。

{% with table.start_time|date:"Y-m-d G:i:s" as start_time %}
    {% ifequal start_time "0000-00-00 00:00:00" %}None{% else %}{{start_time}}{% endifequal %}
{% endwith %}

You can try: 你可以试试:

{% with table.start_time|date:"Y-m-d G:i:s" as time_stamp %}
    {% if time_stamp != "0000-00-00 00:00:00" %}
        <td>{{ time_stamp }}</td>
    {% endif %}
{% endwith %}

There is no if-then control explanation in HTML, or whatever other programming capacities. HTML中没有if-then控件解释,也没有任何其他编程功能。 HTML is a markup dialect. HTML是标记方言。 Writing computer programs isn't conceivable. 编写计算机程序是不可能的。

Css will permit you to pick between styles in light of classes and IDs. CSS将允许您根据类和ID在样式之间进行选择。

You can do this sort of thing with JavaScript, yet you can keep it straightforward with CSS. 您可以使用JavaScript进行此类操作,但可以使用CSS使其简单明了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM