Python: Display either Video or Image File, but it shows both

399 Views Asked by At

I have been working on a social media website where you can upload images and videos and follow other users.

I managed to upload and display uploaded files to the website.

I used FileField to load the image and video files, but when I implement it in my Template it shows both spaces, because there both using the same source url {{ source.file.url }}

models.py

class Post(models.Model):
    title = models.CharField(max_length=150)
    file =  models.FileField(upload_to='uploads/%Y-%m-%d')

models code works perfectly

feeds.html

{% if post.file.url %}
    <video class="video-context" width="500px" height="500px" controls>
      <source src="{{ post.file.url }}" type="video/mp4">
  </video>
  {% endif %}
 
  {% if post.file.url %}
  <img class="image-context" src="{{ post.file.url }}" type="image/jpg" type="image/jpeg">
  {% endif %}

heres a sreenshot empty video player over img

Now the problem is that both File Types are going trough one Model: FileField. The source is {{ source.file.url }} , the HTML is searching both files, but there only either image or video, so what can i do with the other empty file?

How can I hide it?

1

There are 1 best solutions below

0
ttt On

You can use the download link without specifying the file type. <a href src="{{ post.file.url }}">