Home > Enterprise >  Django display a video from model
Django display a video from model

Time:11-23

I have a model that contains a video files uploaded to my media/videos folder. When i am on the admin page i can see the video and play it. I am now trying to render it on a template but it doesn't let me.

Here is my model:

class CheckList(models.Model):

choices = (('', 'Select'), ('Good', 'Good'), ('Not Good', 'Not Good'))
fuel_choices = (('', 'Select'), ('Full', 'Full'), ('3 Quarters', '3 Quarters'), ('Half Tank', 'Half Tank'), ('Almost Empty', 'Almost Empty'))
cargo_choices = (('', 'Select'), ('Cargo Secured', 'Cargo Secured'), ('Cargo Not Secured', 'Cargo Not Secured'), ('No Cargo', 'No Cargo'))

vehicle = models.ForeignKey(Trucks, on_delete=models.CASCADE, blank=True, null=True)
driver = models.ForeignKey(User, on_delete=models.CASCADE)
breaks = models.CharField(null=False, choices=choices, max_length=50, default='Select')
wipers = models.CharField(null=False, choices=choices, max_length=50, default='Select')
wiper_fluid = models.CharField(null=False, choices=choices, max_length=50, default='Select')
cargo = models.CharField(null=False, choices=cargo_choices, max_length=50, default='Select')
cargo_straps = models.CharField(null=False, choices=choices, max_length=50, default='Select')
tires = models.CharField(null=False, choices=choices, max_length=50, default='Select')
oil = models.CharField(null=False, choices=choices, max_length=50, default='Select')
miles = models.IntegerField(null=False, default='0')
gas = models.CharField(null=False, choices=fuel_choices, max_length=100, default='Select')
seatbelt = models.CharField(null=False, choices=choices, max_length=100, default='Good')
date_created = models.DateTimeField(auto_created=True)

# Need video of vehicle
video = models.FileField(upload_to='videos/')

def __str__(self):
    return self.vehicle.nickname

My View that handles the page render:

@login_required()
def report_detail(request, pk):
    checklist = CheckList.objects.get(pk=pk)
    context = {'checklist': checklist}
    return render(request, 'driver/report_detail.html', context)

And the template where i am trying to load the video on:

{% extends 'user_area/base.html' %}
{% block title %}
Fleet Manager
{% endblock title %}

{% block content %}

{% include 'user_area/components/weather.html' %}

{% if user.is_authenticated %}

<div style="margin-top: 30px; margin-left: 50px">
    <h4>Vehicle: {{ checklist.vehicle}}</h4>
    Driver: {{checklist.driver}} <br>
    Breaks Status: {{checklist.breaks}} <br>
    Wipers Status: {{checklist.wipers}} <br>
    Wiper Fluid: {{checklist.wiper_fluid}} <br>
    Cargo Status: {{checklist.cargo}} <br>
    Cargo Straps Status: {{checklist.cargo_straps}} <br>
    Tires Status: {{checklist.tires}} <br>
    Oil Status: {{checklist.oil}} <br>
    Miles Out: {{checklist.miles}} <br>
    Gas: {{checklist.gas}} <br>
    Seatbelt Status: {{checklist.seatbelt}} <br>
    Vehicle Taken out on: {{checklist.date_created}} <br>
    <video width="320" height="240" controls>
<source src="{{checklist.video}}" type="video/mp4">
Your browser does not support the video tag.
</video>
    Your browser does not support the video tag.
</video>

</div>



{% else %}
<a href="{% url 'user_area:home' %}">Please login</a>

{% endif %}
{% endblock %}

Now my question is, if the video is 'saved' under checklist.video, and it's inside my media folder how do i point the template to it so it renders it?

Edit** I changed a few things in the HTML, it is showing the path to the video but it does not play it.

CodePudding user response:

You've to use the url of the video in the templates using checklist.video.url:

<source src="{{ checklist.video.url }}" type="video/mp4">
  • Related