blackwindow
blackwindow

Reputation: 437

If, Else inside html using Django templates

I have a Django project that consists of many html pages.

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. My code is shown below, I used a tag to get the time in date format.

<td>{{ table.start_time|date:"Y-m-d G:i:s"}}</td>

Upvotes: 0

Views: 5589

Answers (4)

blackwindow
blackwindow

Reputation: 437

I just used another tag:

<td>{{ table.start_time|date:"Y-m-d G:i:s"|default:"None"}}</td>

Thanks all!!

Upvotes: 2

asianmartt
asianmartt

Reputation: 31

There is no if-then control explanation in HTML, or whatever other programming capacities. HTML is a markup dialect. Writing computer programs isn't conceivable.

Css will permit you to pick between styles in light of classes and IDs.

You can do this sort of thing with JavaScript, yet you can keep it straightforward with CSS.

Upvotes: 1

Wtower
Wtower

Reputation: 19902

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 %}

Upvotes: 1

argaen
argaen

Reputation: 4245

You can do this with the ifequal template tag.

{% 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 %}

Upvotes: 1

Related Questions