How to Download YouTube Video in Django?

Published On: 14/09/2022 | Category: Django


Hi Dev,

This article goes in detailed on how to download youtube video in django. you can understand a concept of how to download youtube videos in python django. it's simple example of youtube video downloader using django. In this article, we will implement a django youtube video download example.

In Django we will see how to make a YouTube video downloader tool in Django. We will be using pytube module for that.

pytube it is python’s lightweight and dependency-free module, which is used to download YouTube Videos.

Here i explained simply step by step example of how to download youtube video in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 manage.py startapp core
Step 3: Update setting.py

Then update INSTALLED_APPS within our settings.py file to notify Django about the app.

settings.py
....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]
Step 4: Installing the pytube library

In this section first of all we need to install pytube library through below following command:

pip install pytube
Step 5: Creating the Views

In this step, we need to configure views. The youtube page will just be a template and we will help to download to youtube video in backend. open the core/views.py file and add:

core/views.py
from django.shortcuts import render, redirect
from pytube import *
from django.contrib import messages

# Create View
def youtube(request):

    # checking whether request.method is post or not
    if request.method == 'POST':
        
        # getting link from frontend
        link = request.POST['link']
        video = YouTube(link)

        # setting video resolution
        stream = video.streams.get_highest_resolution()
        
        # downloads video
        stream.download()
        messages.success(request, 'Download Successfully!')
        # returning HTML page
        return render(request, 'youtube.html')
    return render(request, 'youtube.html')
Step 6: Creating the Templates

Next, then with your text editor create new templates files: core/templates/youtube.html file and the add:

core/templates/youtube.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tuts-Station.com</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <style type="text/css">
        body{
            background-color: #f7fcff;
        }
    </style>
</head>
<body>
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h4>How to Download YouTube Video in Django? - <span class="text-primary">Tuts-Station.com</span></h4>
                    </div>
                    <div class="card-body">
                        {% if messages %}
                            <div class="alert alert-success alert-dismissible">
                                <button type="button" class="close" data-dismiss="alert">×</button>
                                {% for message in messages %}
                                    {{ message }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <form action="" method="post">
                            {% csrf_token %}
                            <label for="link">Enter URL:</label>
                            <input type="text" id="link" name="link" class="form-control">
                            <button type="submit" class="btn btn-success mt-2">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Step 7: Creating URLs

In this section, we need a urls.py file within the core app however Django doesn't create one for us with the startapp command. Create core/urls.py with your text editor and paste below code.

core/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('youtube', views.youtube, name='youtube'),
]

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

Next, go to the http://localhost:8000/youtube address with a web browser.

I hope it will help you....

Happy Pythonic Coding!