views:

393

answers:

1

I have the following code in one of my Django templates that I want to refactor:

{% ifequal sort_type "set" %} 
 {% regroup cards by set as grouped %} 
{% endifequal %}
{% ifequal sort_type "rarity" %} 
 {% regroup cards by rarity as grouped %}
{% endifequal %}

It does work, but it's really ugly and I want to make it more like this:

{% regroup cards by sort_type as groupedcards %}

But this doesn't work (it just puts them all in a single group called None.) From the documentation, I think it might be trying a dictionary lookup (i.e., calling card["set"] instead of card.set).

Is there a good way to do this in the template, or should I move the regrouping out into the Python code using itertools?

+3  A: 

Ticked in Django bugtracker related to this problem.
And a sample of similar usage of regroup which may help to refactor the code.

zihotki
The similar usage is not the same, because they know the property to regroup by in advance (I only get it at run time). But the ticket is helpful, and seems to indicate that it's not possible to do this.
Kiv