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'),
CodePudding user response:
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)
CodePudding user response:
try this
change this
<video width='400' controls>
<source src='{{videofile.url}}' type='video/mp4'>
Your browser does not support the video tag.
</video>
<br><br>
to
{% for video in lastvideo %}
{% if video.videofile %}
<video width='400' controls>
<source src='{{ video.videofile.url }}' type='video/mp4'>
Your browser does not support the video tag.
</video>
<br><br>
{% endif %}
{% endfor %}
