In this article, we will see how to make a YouTube video downloader tool in Django. We will be using pytube module for that.
Prerequisite:
- pytube: It is python’s lightweight and dependency-free module, which is used to download YouTube Videos.
- Django: It is python’s framework to make web-applications.
Here, we will be using Django as a backend along with pytube module to create this tool. We can install pytube module by typing the below command in the terminal.
pip install pytube
So, let’s dive in to make our YouTube video downloader tool.
First, we will create an HTML design (form) where the user can come and enter the URL of a video which he/she wants to download. We will use Django’s POST method to get that URL (because it is secure). We also need to use csrf token if we are using the POST method. Syntax for csrf token is:
{% csrf_token %}
HTML
<!DOCTYPE html>
< html >
< body >
< h1 >Youtube video downloader</ h1 >
< form action = "" method = "post" >
{% csrf_token %}
< label for = "link" >Enter URL:</ label >
< input type = "text" id = "link" name = "link" >< br >< br >
< input type = "submit" value = "Submit" >
</ form >
</ body >
</ html >
|
Now, it’s time to create a function that receives the video link and downloads that video. You need to import function YouTube from module pytube in views.py file. Now we can define the function to download video.
views.py
Python3
from django.shortcuts import render, redirect
from pytube import *
def youtube(request):
if request.method = = 'POST' :
link = request.POST[ 'link' ]
video = YouTube(link)
stream = video.streams.get_lowest_resolution()
stream.download()
return render(request, 'youtube.html' )
return render(request, 'youtube.html' )
|
Now, we have to define the URL (path) for this function inside urls.py.
Python3
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path( 'admin/' , admin.site.urls),
path( 'youtube' , views.youtube, name = 'youtube' ),
]
|
That is it for the coding part, now you can run the project by python manage.py runserver and head over to http://localhost:8000/youtube to see the output.
Output:

When you click on submit a video will be downloaded in your Django project’s directory.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 May, 2022
Like Article
Save Article