1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 | # put this in a templatetag library:
from django.template import Library
register = Library()
def admin_list_filter_select(cl, spec):
choices = list(spec.choices(cl))
# query_string of a non-all option will contain the name of our param (skipping '?'):
# ?param1=x&fieldname_param=y
key = (k.split('=')[0] for k in choices[-1]['query_string'][1:].split('&')
if k.startswith(spec.field.name)).next()
for c in choices:
# find the option's value by looking after our key and skipping '='
if key in c['query_string']:
c['val'] = c['query_string'].split(key)[1].split('&')[-1][1:]
else:
c['val'] = ''
return {'field_name': key,
'title': spec.title(),
'choices' : choices}
admin_list_filter_select = register.inclusion_tag('admin/filter_select.html')(admin_list_filter_select)
# template:
{% load i18n %}
<label>{% blocktrans with title|escape as filter_title %} By {{ filter_title }} {% endblocktrans %}</label>
<select name="{{ field_name }}">
{% for choice in choices %}
<option {% if choice.selected %} selected="selected"{% endif %} value="{{ choice.val }}">{{ choice.display|escape }}</option>
{% endfor %}
</select>
# modify your admin/change_list.hml to include the templatetag lib and this code: (for example
# in the search block:
...
{% block search %}{% search_form cl %}
{% if cl.has_filters %}
<form action="." method="get">
{% for spec in cl.filter_specs %}
{% admin_list_filter_select cl spec %}
{% endfor %}
<input type="submit"/>
</form>
{% endif %}
{% endblock %}
|
Comments
I found a somewhat simpler method: create a new template admin/filter.html containing:
#
The above code is great - just one small change:
Use - style="width:90%; margin-right:5%;"
to keep the drop down box neatly centred in the filter div.
#