Login

Non-Javascript select list navigator

Author:
shacker
Posted:
March 25, 2009
Language:
Python
Version:
1.0
Tags:
form select picklist
Score:
-1 (after 1 ratings)

If you need a simple select list (picklist) containing all site categories (or some other taxonomic group) and don't want to depend on Javascript, here's how to build a simple category navigator in Django, using HttpResponseRedirect.

 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
# template - assumes an object "all_cats" containing all categories 
# or site sections you want to appear in the select list. 
# Probably best to put the guts of the select loop all on one line.

<form action="" method="post">
   Select another category:
   <select name="type">
       {% for cat in all_cats  %}
            <option value="{{ cat.slug }}" 
            {% ifequal cat.slug category.slug %}
            selected="selected"{% endifequal %}
            >{{ cat.title }}
       {% endfor %}
   </select> 
   <input type="submit" name="submit" value="Go" />
</form>


# views.py
# Handle the redirection.  
# Assumes a reversible URL in urls.py called events_cat that takes
# cat_slug as an arg. Season to taste.

from django.http import HttpResponseRedirect

    if request.POST.get('type'):
        return HttpResponseRedirect(reverse('events_cat', 
            kwargs={'cat_slug':request.POST['type'],}))

More like this

  1. CategoriesField by fongandrew 6 years, 1 month ago
  2. Template tag to handle navigation item selection by SmileyChris 5 years, 11 months ago
  3. Hat-trick for getting categories related to an object group by pablo_PXL 4 years, 7 months ago
  4. Timezone choice field for select box by jmunro 3 years, 6 months ago
  5. Navigation list from YAML file by sean2000 7 years, 7 months ago

Comments

Please login first before commenting.