Skip to main content

Minimal filter and search functionality for django list views.

Project description

django-minifilter

The django-minifilter package provides minimal filter functionality for list views, including:

  • a filter search box

  • filter links

The package is compatible with django's pagination.

Here's an example of a very basic list-view, showing a search box that filters by name, and links that filter by year and month:

basic example of filtered list view

Installation

The django-minifilter package is available on pypi and can be installed via pip:

pip install django-minifilter

or via pipenv:

pipenv install django-minifilter

Then add minifilter to INSTALLED_APPS in your django settings.

Quick example

Suppose we have the following simple model, part of an application called myapp:

from django.db import models
from django.utils import timezone

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    date = models.DateField(default=timezone.now)

Here's how we would create a generic list view with a search box filter, links that filter by year and month, and pagination:

from django.views import generic
from minifilter.mixins import FilterMixin
from myapp.models import MyModel


class MyListView(FilterMixin, generic.ListView):
    model = MyModel
    template_name = 'myapp/mymodel_list.html'
    paginate_by = 10
    search_fields = ['name']
    filter_parameters = [
        # (url-parameter-name, lookup)
        ('year', 'start_date__year'),
        ('month', 'start_date__month'),
    ]

And here's a simple template for the above:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My filtered list view</title>
</head>
<body>
{% include 'minifilter/includes/search.html' %}
{% include 'minifilter/includes/parameters.html' %}
<div>
    <ol>
        {% for obj in page_obj %}
            <li>{{ obj.name }} - {{ obj.date }}</li>
        {% endfor %}
    </ol>
</div>
{% include 'minifilter/includes/pagination.html' %}
</body>
</html>

Instead of a generic class-based view, we could also create a function-based list view:

from django.template.response import TemplateResponse
from django.core.paginator import Paginator
from minifilter.filters import search_filter, parameter_filter
from myapp.models import MyModel


def my_list_view(request):
    queryset = MyModel.objects.all()
    # filter by search term
    queryset, search_form = search_filter(
        queryset=queryset, request=request,
        search_fields=['name'])
    # filter queryset based on url query parameters
    queryset, parameter_choices = parameter_filter(
        queryset=queryset, request=request,
        filter_parameters=[
            ('year', 'start_date__year'),
            ('month', 'start_date__month')])
    # paginate filtered queryset
    # see: https://docs.djangoproject.com/en/stable/topics/pagination/
    paginator = Paginator(object_list=queryset, per_page=10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    # build response
    return TemplateResponse(
        request, template='myapp/mymodel_list.html',
        context=dict(
            page_obj=page_obj,
            parameter_choices=parameter_choices,
            search_form=search_form,
        )
    )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_minifilter-0.0.5.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

django_minifilter-0.0.5-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file django_minifilter-0.0.5.tar.gz.

File metadata

  • Download URL: django_minifilter-0.0.5.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.6

File hashes

Hashes for django_minifilter-0.0.5.tar.gz
Algorithm Hash digest
SHA256 5ef9985107a618444ee4fe062aba4df34cb8ce561596583d738fd1931f772a9a
MD5 9e1541e70b649b9c71637bf6a5c06400
BLAKE2b-256 06e61327e70ea591c0e9b1f21376d8edb572beac7732300de8f22412a2831fd2

See more details on using hashes here.

File details

Details for the file django_minifilter-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_minifilter-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6c2fd4d9bd27ad6cb43742afb61f4a7834cf13fbd7b1601cba64548681de4587
MD5 218f6b3d55326d19b7029c144642f371
BLAKE2b-256 28f6b796fb282dd91e8b0322c2b6d6031ec1dc0b5346894d35301096e9bc38e3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page