我有一個模型,其中包含上傳到我的 media/videos 檔案夾的視頻檔案。當我在管理頁面上時,我可以看到視頻并播放它。我現在試圖在模板上呈現它,但它不允許我。
這是我的模型:
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
我處理頁面渲染的視圖:
@login_required()
def report_detail(request, pk):
checklist = CheckList.objects.get(pk=pk)
context = {'checklist': checklist}
return render(request, 'driver/report_detail.html', context)
以及我嘗試加載視頻的模板:
{% 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 %}
現在我的問題是,如果視頻是在 checklist.video 下“保存”的,并且它在我的媒體檔案夾中,我如何將模板指向它以便呈現它?
編輯** 我在 HTML 中更改了一些內容,它顯示了視頻的路徑,但不播放。
uj5u.com熱心網友回復:
您必須使用以下模板中的視頻網址checklist.video.url:
<source src="{{ checklist.video.url }}" type="video/mp4">
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/363095.html
