How to Create a Video Search Engine with Python in Django


In this article, we show how to create a video search engine.

This is a search engine that retrieves video files and can display videos, which can then be watched by a user.

The largest video search engine in the world is youtube.

Youtube is a mammoth site that contains literally countless videos. The videos (or the file pathways) are stored in a database. Google uses MariaDB.

During this article, we create a database named Video, which stores video files.

We then have a video page where users can go and watch videos. On this video page, we a search bar where users can enter in a search query to find the video they would like to watch, just as on youtube.

So let's go right into building this video search engine. File

So the first thing we need to do is create a database table that holds the videos for our site.

Of course, you don't have to put all of your videos in one single database. It can be several databases, but for this video, for simplicity, we definitely will just make it a single database table.

So we create a database table called Video.

It's a single database table that has just 3 fields: name (name or title of the video), description, and videofile (the pathway to the file).

This is shown in the code below.

So this is what we have.

We created a database table called Video.

It has name, description, and videofile columns.

The name field simply represents the name, or title, of the video.

The description field represents the description of the video (just like youtube has a description field underneath the video.

The videofile field represents the pathway to the video file, so that when we are using a video player to play out the video, we know the pathway to the video.

Any time you are creating a video uploader site, you have to make sure that the files that are entered video file formats. Django does not have a video field as it has an ImageField. Therefore, you have to create your own custom function to make sure that only video files can be uploaded. Since the field is a FileField, any file can be entered such as a pdf file or a Word or an image file, which is unintended for a video site. Therefore, in the file, we write a validating function for video files.

So those are the essential categories. Of course, with a site like youtube, there's more fields, such as keywords for the video for search, but we keep it simple. We simply will search videos based on the name of the video. A more advanced technique is also looking at keywords and also the description. But, for simplicity, we don't do that here. File

So as mentioned before, we have to create a validator function to validate to make sure the file being uploaded is really a video.

For our video site, we play videos through the HTML video player. This video player only supports .mp4, .webm, and .ogg video files.

Therefore, we write a validator function that only accepts these 3 video formats.

The code for this validator function is shown below.

In this video, we import ValidationError from django.core.exceptions.

ValidationError allows us to raise a validation error if some input entered into the form doesn't meet the validation guidelines for the custom function we create.

So we create a function called validate_video and pass in the parameter, value.

Because value is from a FieldField, we have to convert it to a string in order use functions such as endswith().

So if the file uploaded does not end with .mp4, .webm. or .ogg, we raise a validation error. If not, we accept the file. File

So now let's go to the file.

Remember that we have to create a form on the video page where users view the videos.

This form contains one element, the search field, where a user can enter a query in to search for videos.

So it's very basic.

The contents of this form is shown below.

So we create a form called VideoSearchForm.

This form has a single field named search.

This search field is a simple CharField that can have a max_length of up to 100.

And this is all for the file. File

So now let's go to the file.

In this file, we import the database table and form that we created.

We then get all objects of the database.

We obtain the search query that the user has entered into the search field and then look to see if anything in the videos database table matches that.

The contents of the file is shown below.

In this file, we must import render, since we're rendering a template.

We import the Video database table from the file.

We import Videoform from the file.

We then create a function-based view, videos().

We create a variable called searchvalue and set it equal to '', or else when we use the searchvalue variable, it will cause an error.

We create a variable called form and set it equal to Videoform(request.POST or None).

We then make sure the form data is valid.

We then set the variable, searchvalue, equal to, Video.objects.filter(name__icontains= searchvalue)

What this line does is it looks through all of the objects in the Video database talbe to see if any contain the search query entered by the user. icontains is case-insensitive, while contains is case-sensitive.

We then pass the form variable and searchresults variable into the context dictionary.

We then render the videos.html template file, passing into the context dictionary into it.

template file

Lastly, we have the template file, videos.html.

In this file, we show the search field form.

And we create a table of all objects in the database table.

When a visitor enters in a search query, only videos containing that search query will be returned.

The contents of this template file is shown below.

So in our template file, we have an h1 tag with Videos in between the tags.

We then have our form.

The action attribute of the form is equal to "" because we want the data to stay on the current page. And the method is set equal to "POST", because we want the data posted to the actual page, rather than attached to the URL.

We put in the line, {% csrf_token %}, for security to prevent cross site request forgery.

We then render out the form we created in table format, because like youtube, the form is on a single line.

We then add the submit button for the form.

We then close the form.

We then render out the table.

We create a for loop that loops through all results that match the search query that the user has entered.

And this is all that is required to create a video search engine with Python in Django.

So just so that you can see this form, I populate it with 3 videos, one for Carmelo Anthony Missed Dunk, another for Carmelo Anthony Foot Locker Commercial, and another for Elon Musk Spacex Tour.

This is the database table before we enter in a search query, shown below.

Video search engine in Django

Now we enter in the search query, 'carmelo anthony foot locker'.

We get the following output, shown below.

Video search engine with search query results in Django

And this is all that is necessary to build a basic but working example of a video search engine with Python in Django.

Related Resources

How to Create Dynamic URLs in Django

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django

HTML Comment Box is loading comments...