Add context to list views for sidebar links and modifying querysets with those links.
Project description
Django ListView Filters
Description
Add context to list views for sidebar links and modifying querysets with those links.
Purpose
Attempting to duplicate the functionality of the ModelAdmin
List Filter capabilities.
This is largely a copy-paste of the source code for that modified to work outside of the admin interface. For example, references to model_admin
have been removed.
Additional Functionality
A few customizations are added.
Add 'Clear Filter' Context
Allow for replacing the 'All' link with a button that clears the parameter from the query.
Only List Lookups With Matches
Allow for filtering of list for sidebar to only those with matches. That way empty links aren't taking up valuable space.
Add Count to Context
Add the count of number of objects to each link that can be shown in the template.
Configuration
Model
from django.db import models
class Author(models.Model):
name = models.CharField("Author's Name", max_length=100)
birthday = models.DateField("Author's Birthday", blank=True)
class Book(models.Model):
title = models.CharField("Book Title", max_length=150)
author = models.ForeignKey(Author, on_delete=models.PROTECT)
Class-based View
from django.view.generic import ListView
from django-listview-filters import RelatedFieldListViewFilter
class AuthorListView(ListView):
context_object_name = "author"
queryset = Author.objects.order_by("name")
class BookListView(ListView):
context_object_name = "book"
queryset = Author.objects.order_by("title")
list_filter = [
('author', RelatedFieldListViewFilter)
]
Template
{% for filter_name, filter_objects, clear_fragment in filter_list %}
<div>
{{ filter_name|title }}
<a href="{{ clear_fragment }}">clear filter</a>
</div>
<ul>
{% with filter_objects|dictsort:"display" as display_list %}
{% for item in display_list %}
<div>
<a href="{{ item.query_string }}">
{{ item.display }}
</a>
</div>
{% endfor %}
{% endwith %}
</ul>
</div>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_listview_filters-0.0.1b1.dev3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb51d3cb355331f6a2b948724254dfad5624e253aeefa924553bfa2d38364d51 |
|
MD5 | 92648210fc58befbc1f10a11e87bccbe |
|
BLAKE2b-256 | 422b910bfa52289a66d6db6709d0e9859f89f95b6dd590c9b094b872abe8df46 |
Hashes for django_listview_filters-0.0.1b1.dev3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11e21be7c1a7324f86cf48b52d4b4e154a2224b3871064465a110764bf7ecff4 |
|
MD5 | a10b31bb1739ab90d93376dec79e5075 |
|
BLAKE2b-256 | af531bd2a52232e9695a27014caf3703bb57056a3c3d44715e4b8911f09934b8 |