Adarsha B.G
Adarsha B.G

Reputation: 132

How to display video using Django Detail view?

The following is my model.py

class VideoUpload(models.Model):
    user                = models.ForeignKey(User, on_delete=models.CASCADE)
    video               = models.FileField(null=False, upload_to='my_videos/videos/%Y/%m/%d/')
    video_thumbnail     = models.ImageField(null=False, upload_to='my_videos/thumbnails/%Y/%m/%d/')
    video_title         = models.CharField(null=False, max_length = 120)
    slug                = models.CharField(null=False, max_length=50)
    date_uploaded       = models.DateTimeField(default=timezone.now)

the following is my views.py which has both listView and detailView

#listView

class VideoHome(ListView):
    model = VideoUpload
    template_name = 'videoTube/videoDetail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["my_video_list"] = VideoUpload.objects.filter(user=self.request.user)
        context["other_video_list"] = VideoUpload.objects.exclude(user=self.request.user)
        return context

#detailView

class VideoDetailView(DetailView):
    model = VideoUpload
    template_name = 'videoDetail.html'

my url config is shown below

path('<pk>/', VideoDetailView.as_view(), name='video'),

Template

<body>
    <video width="320" height="240" controls>
      <source src="{{ videoupload.video.url }}" type="video/mp4">
    Your browser does not support the video tag.
    </video>
</body>

learnerz is my project name inside learnerz/url.py i have included 'videoTube/'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('videoTube/', include('videoTube.urls')),
]

Please let me know how to make the video display. Thank you in advance

enter image description here

Upvotes: 0

Views: 109

Answers (1)

Lemon.py
Lemon.py

Reputation: 819

You use a video tag, and access the video URL using the object. Place the code below into your HTML file for your detail view

    <video width="320" height="240" controls>
      <source src="{{ object.video.url }}" type="video/mp4">
    Your browser does not support the video tag.
    </video>

Upvotes: 1

Related Questions