Django Filter
A library like django form to filter queryset.
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.
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
Step 3: Add Models and Register in Models in admin
Update myapp/models.py
Update 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
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')
Step 8: Create HTML template
Create bootstrap base.html
Create books.html
Step 9: Create views function in views.py
Step 10: Set up URLs
Create myapp/urls.py
Update 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
Open book site and filter test: http://127.0.0.1:8000/books