Login

admin filters as select boxes

Author:
gzy
Posted:
October 13, 2009
Language:
Python
Version:
1.1
Tags:
filter admin filterspec
Score:
-1 (after 1 ratings)

This templatetag let's you output a form with select boxes instead of the ul's for filters. Uses some hacks to get the param names out of query strings.

This would be a lot easier if filterspecs defined params instead of query strings (if filter tag would handle the encoding)

 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 %}

More like this

Comments

softmechanics (on January 6, 2010):

I found a somewhat simpler method: create a new template admin/filter.html containing:

{% load i18n %}            
<h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
<div align="right">
  <select onChange="javascript:window.location = this.options[this.selectedIndex].value;" style="width: 80%">
    {% for choice in choices %}
      <option {% if choice.selected %}selected{% endif %} value="{{ choice.query_string|iriencode }}">
        {{ choice.display }}
      </option>
    {% endfor %}           
  </select>                
</div>

#

gamesbook (on October 19, 2010):

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.

#

Please login first before commenting.