I have a ListView
which I display using HTML <table>
element. Each column is a field on model, while each row is a particular model instance.
For this view I implemented search over multiple model fields (via GET form with query parameter search
), filtering for each model field (via GET forms with query parameters filter--{{ field.name }}-{{ filter-type }}
) and ordering over multiple fields (via query parameter order
). I am planning to add pagination and ability for user to set page size (so soon two more query parameters).
My question is: is there a simple approach to handle preserving existing unchanged GET values in both anchors and GET forms?
For context:
All three can be active at the same time, so paths can look like all of:
/example/
/example/?filter--service_expires_at-datetime_from=2024-02-27T09%3A31&filter--service_expires_at-datetime_to=
/example/?filter--username-icontains=abc&search=test
/example/?order=-username,service_expires_at&search=test&filter--username-icontains=abc
Search and filters have both links (to clear search or particular filter) and GET forms (to set search or particular filter). Order has links, which modify order GET parameter to either add ASC or DESC sort (or remove both) over particular column.
My problem is that preserving GET parameters got quite cumbersome – for example, when I want to change order value, I need to take care to preserve existing search and all filter values.
Currently I am handling this by dynamically generating both get strings and hidden form inputs in view and handing them to template through context (one context variable for each of search, filter, order and each of form, link). And then using them in template as:
<form method="GET">
{{ search_form }}
{% autoescape off %}
{{ preserve_q_string_in_search_form }}
{% endautoescape %}
<button
type="submit"
>
????
</button>
</form>
or
<a
href="?order={{ f.q_sort_desc }}&{{ preserve_q_string_in_order_href }}"
>
DESC
</a>
My problem with this approach is that it is both quite cumbersome (I need to write logic both for all different queries and both usage via form and via href) and rather brittle (I need to remember to include correct variable from context in all links and forms).
I also tried to handle this via template include and few custom filters:
{% load core_filters %}
{% for getkey in request.GET %}
{% if except_prefix and getkey|startswith:except_prefix %}
{% else %}
<input class="hidden" name="{{ getkey }}" value="{{ request.GET|get:getkey }}"/>
{% endif %}
{% endfor %}
@register.filter
def get(dict: dict, key):
"""Returns value of key in dict if present, else ""."""
return dict.get(key, "")
@register.filter
def startswith(string: str, prefix: str):
return string.startswith(prefix)
But found this particular implementation rather finicky and too hackish.