IMPLEMENTATION OF SEARCH BAR USING DJANGO (in any website)
Creation of project with a name after activating virtual environment of django.
$ django-admin startproject search_bar
Now,we have to create an app using command line like shown below
$ python manage.py startapp stu
settings.py file
After the completion of creating app, we have to update app name in INSTALLED_APPS of
settings.py file to indicate django that we have created an app as below.
models.py file:
Lets move to models.py file. Here I am taking the student information as my data ,so the fields I am using are name, class,section of the student .
Now the models.py file look like
Applying migrations
Now let us apply migrations to create database and also create superuser to access the database.
$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py createsuperuser
views.py file
In views.py file we should have two functions for home page and search page.These represent the
actions which they perform.Here the search is implemented based on the clas numbers.
Here the variable srh receives the query from the client and it filters the objects and checks if there is
any object which matches the query.
If exists it returns the filtered object to stuli variable.
urls.py file:
Now we to give path in search_bar/urls.py file by adding the following code.
admin.py file:
Let us now register the model in the admin.py file.
Creating templates for home page and search page:
For this first we have to create templates folder and then create the corresponding html pages in
that folder, as below.
Also add templates root directory in settings.py file
HTML FILES CREATION:
home page.html
search page.html
Creation of data in database:
As you can see I have created 18 records in my database, the same as shown below.
Final Output Of Search Bar:
Once again ,apply migrations and you can see the output by
$ python manage.py runserver
(or)
Output
We have searched for the students of class 1 .So in the database we have 3 students of class 1 and it has given the information required.
So this is the end of search bar implementation.
Hope you have enjoyed….