Using if statement in django template to detect a NULL
My web application stores dances and the YouTube link to that dance. The table shows the dance name and a link to the video which is passed to a new page to show the embedded video. This all works fine but some dances do not have a video and the return from the database for video_id is NULL.as below
http://localhost:8000/video_test/HjC9DidEwPc,%20Big%20Blue%20Tree --- with video or http://localhost:8000/video_test/NULL,%20Baby%20Kate ---- with no video
I want include a test for the null in the template which tabulates the dances so that the link does not appear if no video
tabulated output is the word video is a link to video_test
Column A | Column B |
---|---|
The dance name | Video |
The dance name | Video |
I have tried using {% if i.video == NULL %} is NULL, is None, but none work.I have looked at various other questions which seem to suggest that one of the above should work. I either get an unable to parse error or the if statement has no effect. . Model
class Dances(models.Model):
name = models.CharField('name', max_length=120)
video_id = models.CharField('video_id', max_length=50)
level = models.CharField('level', max_length=3)
def __str__(self):
return str(self.name)
view
def video_test(request, id, name):
vid_id= id
d_name = name
return render(request, 'alineapp/video_test.html',{'vid_id':vid_id, 'd_name':d_name})
Template
<!-- Table for Beginner dances -->
<table border="1" cellspacing="2" cellpadding="2">
{% for i in beg_list %}
<tr>
<td>{{ i.name }}</td>
{% If i.video !== NULL %}
<td><a href="{% url 'video_test' i.video_id i.name %}">Video</a></td>
{% else %}
<td> None </td>
{% endif %}
</tr>
{% endfor %}
Did you try not none
?
{% if i.video is not none %}
<td><a href="{% url 'video_test' i.video_id i.name %}">Video</a></td>
{% else %}
<td> None </td>
{% endif %}