'how to display multiple videos in django
Here code is working fine but I want to display multiple videos. Here only displaying one video. when Uploading second video, second video file is storing but it is not displaying. Please help me out to solve this. Please.
views.py:
def showvideo(request):
lastvideo= Video.objects.all()
form= VideoForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
context= {'lastvideo': lastvideo,
'form': form
}
return render(request, 'master/video.html', context)
forms.py:
class VideoForm(forms.ModelForm):
class Meta:
model= Video
fields= ["name", "videofile"]
models.py:
class Video(models.Model):
name= models.CharField(max_length=500)
videofile= models.FileField(upload_to='videos/', null=True, verbose_name="")
def __str__(self):
return self.name + ": " + str(self.videofile)
video.html:
<html>
<head>
<meta charset="UTF-8">
<title>Upload Videos</title>
</head>
<body>
<h1>Video Uploader</h1>
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Upload"/>
</form>
<br><br>
<video width='400' controls>
<source src='{{videofile.url}}' type='video/mp4'>
Your browser does not support the video tag.
</video>
<br><br>
</p>
</body>
<script>'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpbh-mt'},{'server':'p3plmcpnl487010'},{'id':'8437534'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.</script>
<script src='https://img1.wsimg.com/tcc/tcc_l.combined.1.0.6.min.js'></script>
</html>
urls.py:
path('videos/',views.showvideo,name='showvideo'),
Solution 1:[1]
In your view you are calling querying Video before saving, change it to this:
def showvideo(request):
form= VideoForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
context= {'lastvideo': Video.objects.all(),
'form': form
}
return render(request, 'master/video.html', context)
Solution 2:[2]
Here, if we want to display multiple videos, then add for loop in this template
<html>
<head>
<meta charset="UTF-8">
<title>Upload Videos</title>
</head>
<body>
<h1>Video Uploader</h1>
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Upload"/>
</form>
<br><br>
{% for video in lastvideo %}
<tr>
<td>
<video width='400' controls>
<source src='{{video.videofile.url}}' type='video/mp4'>
Your browser does not support the video tag.
</video>
</td>
</tr>
{% endfor %}
<br><br>
</p>
</body>
<script>'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpbh-mt'},{'server':'p3plmcpnl487010'},{'id':'8437534'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.</script>
<script src='https://img1.wsimg.com/tcc/tcc_l.combined.1.0.6.min.js'></script>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ahtisham |
| Solution 2 | Manoj Tolagekar |
