Login

Non-Javascript select list navigator

Author:
shacker
Posted:
March 25, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 3 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.