Django Filter

Adi Ramadhan
2 min readJun 20, 2022

A library like django form to filter queryset.

django-filter

Official docs django-filter.

Project file structure.
templates folder, base.html, books.html, myapp/filters, myapp/urls.py files are manually created (new files), other files are auto-generated.

final file project structure

Step 1: Preparation, Create Django Project, Inital Migration
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.2 django-filter
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate

Step 2: Create Django Apps and Register django-filter
Create apps: python manage.py startapp myapp
Add myapp to INSTALLED_APPS in myproject/settings.py

INSTALELD_APPS in myproject/settings.py

Step 3: Add Models and Register in Models in admin

Update myapp/models.py

myapp/models.py

Update myapp/admin.py

myapp/admin.py

Step 4: Makemigrations and Migrate
Make migrations: python manage.py makemigrations
Migrate: python manage.py migrate

Step 5: Create Superuser
Create superuser: python manage.py createsuperuser
Type username, email password and retype password

Step 6: Create filters.py in myapp
Create filters.py

myapp/filters.py

Step 7: Set up Templates Folder
Create folder templates in myapp

Add template directory in TEMPLATES parameter in myproject/settings.py

Path(BASE_DIR, 'myapp', 'templates')
TEMPLATES in myproject/settings.py

Step 8: Create HTML template
Create bootstrap base.html

myapp/templates/base.html

Create books.html

myapp/templates/books.html

Step 9: Create views function in views.py

myapp/views.py

Step 10: Set up URLs

Create myapp/urls.py

myapp/urls.py

Update myproject/urls.py

myproject/urls.py

Step 11: Run Server and Testing
Run Server: python manage.py runserver
Open admin site: http://127.0.0.1:8000/admin
Add some books

admin site

Open book site and filter test: http://127.0.0.1:8000/books

book list wit filter

--

--