I\'m working on a Django project with a ListView
that has both a search form, known in the view\'s context as search_form
, and a filter form, fil
I finally solved this by writing a custom get
filter as described in Django template how to look up a dictionary value with a variable:
from django import template
register = template.Library()
@register.filter
def get(dictionary, key):
return dictionary.get(key)
I updated _search.html
as follows:
{% load get %}
Now, if I try to search a filtered result, it works as expected:
Note that this also works fine for the filters that are not applied - these have the value None
instead of an empty string - without any need to filter them out in the form.