How to upload videos in Django – Django tutorials

how to upload videos in Django - django tutorial - www,devpyjp.com

Hello, welcome to another great tutorial in Django. In this tutorial, you will learn how to upload videos in Django. I hope this tutorial definitely helps you in your Django projects or ideas.

What you learn here

  • Create a model for Vidoes
  • Upload a videos from admin panel
  • Creating views for display and upload videos
  • Upload videos from Templates ( HTML Pages )

Let’s build the video uploading project in Django. If you don’t know how to start a Django project and start a Django app please follow these links.

Required knowledge

Build a video uploading project in Django

I created a new app called “video_content“, add it in the settings.py file.

INSTALLED_APPS = [
    
    'video_content',
]

Now configure the static files, templates and media files in settings.py file. And also configure them in urls.py file to work media files and static files properly.

settings.py

# TEMPLATES

TEMPLATES = [

'DIRS': [os.path.join(BASE_DIR,'templates')],

]

# STATIC FILES (OPTIONAL)

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static") 
]

STATIC_ROOT = os.path.join(BASE_DIR,'assets')

# MEDIA FILES

MEDIA_URL ='/media/'

MEDIA_ROOT = os.path.join(BASE_DIR,'media')

Now configure the urls.py file.

from django.urls import path
from video_content.views import upload_video,display

from django.conf.urls.static import static
from django.conf import  settings



urlpatterns = [
   
    path('upload/',upload_video,name='upload'),
    path('videos/',display,name='videos'),

]

    
urlpatterns  += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

You must create the media folder, static folder, and template folder outside of the project folder.

1. Create a model for videos uploading in Django

Now we need to create a model for uploading videos and save them in the database. Here I created a few fields, you can add more fields according to your project.

models.py

from django.db import models

# Create your models here.


class Videos(models.Model):
    title = models.CharField(max_length=100)
    video = models.FileField(upload_to='videos/')
    
    class Meta:
        verbose_name = 'video'
        verbose_name_plural = 'videos'
        
    def __str__(self):
        return self.title
    
  

Now its time to migrate our model and create superuser to access the admin panel. and also register the video model in the admin.py file.

To migrate the video model and create a superuser execute the below commands in cmd or terminal.

2. Upload videos from Django Admin

python manage.py makemigrations

python manage.py migrate

python manage.py createsuperuser

# Enter your credintials to register as admin.

python manage.py runserver

Now add a Video model in the admin.py file to register our model.

from django.contrib import admin

# Register your models here.
from .models import Videos

admin.site.register(Videos)

Now login to your Django admin panel and upload a video.

3. Creating views to upload and display videos

Now its time to create the views for uploading and display the videos in the HTML pages. copy and paste the code views.py.

from django.shortcuts import render,redirect

from .models import Videos

# Create your views here.

def upload_video(request):
    
    if request.method == 'POST': 
        
        title = request.POST['title']
        video = request.POST['video']
        
        content = Videos(title=title,video=video)
        content.save()
        return redirect('home')
    
    return render(request,'upload.html')


def display(request):
    
    videos = Videos.objects.all()
    context ={
        'videos':videos,
    }
    
    return render(request,'videos.html',context)

Here we create a display and upload_video views to display all the videos and upload the videos from the user end. we already configure the views in urls.py file.

4. Creating Templates to upload and display videos

Now create the templates videos.html and upload.html files in the template folder. You must follow the same template names which we added in views.

upload.html

<form action="" method="POST">{% csrf_token %}

    <input type="text" name="title" placeholder="Enter Video Title here"><br>
    <input type="file" name="video" accept="video/*">
    <button type="submit"> Upload New Video </button>

</form>

Here we restrict the user to upload video in the .mp4 format only from the front end using accept attribute.

videos.html

let’s show the all the videos in our Database which were uploaded by us.

<div>

{% for video in videos %}
<video width="500" height="500" controls >
<source src="{{video.video.url}}" type="video/mp4">
<b>{{video.title}}</b>
</video>

</div>

Now run the server to upload a video from the HTML pages using the following command in cmd or terminal.

python manage.py runserver

Now enter the URL according to the url.py file in the browser.

Here you can upload .mp4 format video and click on the upload New Video Button to upload it to the database.

Once you complete the video uploading processing then now run the other URL videos in the browser to view our videos.

If you play the video, then it will look like below. Let’s play the videos, Enjoy!

You can do more customizations and stylings and colorings to the front-end, but I gave the good functionality to work with videos uploading in Django.

Conclusion

Successfully we are done uploading videos in the Django project. I hope this tutorial will help you a lot. To read more read the docs.

If you have any doubts please comment below and to get more notification on our python Django tutorials and machine learning tutorials to subscribe to our newsletter for Free.

5 thoughts on “How to upload videos in Django – Django tutorials”

Leave a Reply