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 startapp core
Step 3: Update

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

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/ file and add:

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
        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:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <style type="text/css">
            background-color: #f7fcff;
    <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"></span></h4>
                    <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 %}
                        {% 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>
Step 7: Creating URLs

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

from django.urls import path
from . import views

urlpatterns = [
    path('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.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    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 runserver

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

I hope it will help you....

Happy Pythonic Coding!