djangodjango-templatesdjango-template-filters

django "regroup by" controlled from view


How do I control by what my list gets regrouped from the view? I have a page with a drop down for attributes the list can be grouped by. Once an attribute selected it goes into the view and it will generate the list - I want the template to pick up the regroup attribute somehow.

{% regroup cities by country as country_list %}

this works, however when I pass the 'country' as a string from the view, it doesn't:

view:

return render_to_response('parts/action_items.html', 
{
'action_item_list': action_items,
'view_filter': 'country'
}, template.RequestContext(request))

template:

{% regroup cities by view_filter as country_list %}

What do I pass into it to make it work?


Solution

  • It isn't possible to use a variable when using the template tag, because the regroup attribute is treated as a string.

    You could regroup the list in the view instead. Importing the regroup tag in your view might work. If that doesn't, you can implement the functionality yourself. See this question for more info.