How to Implement the Django filters

||
Posted 3 years, 9 months ago
||
Views 597
||
3 min read
1 reaction

GET STARTED WITH DJANGO FILTERS:

Django filters allows users to filter down a queryset based on model’s fields

 Requirements:

 To use django filters the basic requirements are:

  •  Python :3.5 and above version

  •  Django:2.2 and above version

 

Let us now learn the functionality of django filters through an example.

For this,

  • Lets create a project named sample

  • In that create an app  named products

 

     Now install django-filters using the below command

    $ pip install django-filter

 

STEP 1:

   After installing django-filters , go to settings.py file and add ‘django_filters’ , app name (products) to INSTALLED_ APPS as shown below

INSTALLED_APPS = [

    .......

    .......

    'django_filters',

    'products'

]

STEP 2:

Now in the app (products)  folder  go to the models.py file and add the details of the product as shown below 

Details of the product :     

  •  Name of product

  •  Price of  product

  •  Description of product

  •  Manufactured date

   

 After adding details to models.py file it should look as below:

from django.db import models


class Product(models.Model):

    name = models.CharField(max_length=255)

    price = models.DecimalField( max_digits=5, decimal_places=2)

    description = models.TextField()

    manufactured_date = models.DateField()

 

 

STEP 3:

      After creating models.py file  we should create filters.py file in the app (products) folder to implement django filters

  

In this example, I want to filter the product based on

  •  Price

  •  Manufactured date

 

Create the filters.py file with the fields required as shown below:

import django_filters 

from . models import Product


class ProductFilter(django_filters.FilterSet):

    class Meta:

        model = Product

        fields = ['price', 'manufactured_date']

 

STEP 4:

    Now let's go to views.py file and write the code as shown below.

 

from django.shortcuts import render

from .models import Product

from .filters import ProductFilter


def home(request):

    f = ProductFilter(request.GET, queryset=Product.objects.all())

    return render(request, 'template.html', {'filter': f})

STEP 5:

     Now we have to set a path and establish a connection to the views.py file, so let’s go to the urls.py file and establish the path as shown below

 

from django.contrib import admin

from django.urls import path

from . import views


urlpatterns= [

   path(' ', views.home,name='home')

]

 

STEP 6:

      Now create a folder named templates inside the app (products)folder,now inside templates folder create a file named template.html 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <div class="card card-body filter">      

         <form method="get" class="filter">

            <h1>Sample Filter Form</h1>

             {{ filter.form.as_table}}

             <button type="submit">Search</button>    

          </form>     

    </div>

    <div class="row">

        <div class="col-md-4">

            <div class="card card-body">

                <table class="table table-sm">

                    <tr>

                        <th>Title     </th>

                        <th>Price</th>

                        <th>Description</th>

                        <th>Manufactured date  </th>     

                    </tr>

                    {% for obj in filter.qs %}

                    <tr>

                        <td>  {{ obj.name  }}      </td>

                        <td>  {{ obj.price }}      </td>

                        <td>  {{ obj.description}} </td>

                        <td>  {{obj.manufactured_date}} </td>

                         {% endfor %}

                </table>

            </div>

        </div>

    </div>

</body>

</html>

And we can also apply css to the html file

STEP 7:

   Finally apply migrations and run the command $ python manage.py runserver 

STEP 8:

  Go to local host:   http://127.0.0.1:8000, you can see the output as:

 Without filter

 

     

After applying filter:


1 reaction

Discussion


Joined on Oct. 5, 2020

Latest Videos