Is there such thing as a VideoField in Django?

Viewed 4047

So I have been making a site recently, and I want a way for people to upload videos and for me to display them, just like I do with an ImageField. However I have found that VideoField is not a thing. I have used FileField so far, and it is uploading to the right folder, however I don't know how to display that video like an ImageField.

I want to use something specifically made for video files, but so far I have't found anything online about VideoFiles.

Here is my model.

class Post(models.Model):
    # ... other things here
    video_file = models.FileField(upload_to='post_files',blank=True,null=True)

So my question is: is there a VideoFile in Django?

3 Answers
  1. There is no VideoField in Django.
  2. You can use FileField for sure.

In your template just add the following:

<video width="500px" height="500px" controls>
     <source src="{{ post.video_file.url }}" type="video/mp4">
</video>

Hope this helps.

In addition of Priyanshu answer's, if you need to display mp4 in Django admin you can use this code :

class MyVideoAdmin(admin.ModelAdmin):
    list_display = ('id', 'video_tag', 'comment', )

    def video_tag(self, obj):
        if obj.video:
            return format_html('<a href="{}">Link to video</a><video height="200px" controls><source src="{}" type="video/mp4"></video>'.format(obj.video.url, obj.video.url))
    video_tag.short_description = 'Video'

admin.site.register(MyVideo, MyVideoAdmin)

I'd go with django-embed-video. It's for beginner-intermediate devs who want to play around with videos on their project.

Related