fromitertoolsimportgroupbyfromdjangoimporttemplatefromdjango.templateimportTemplateSyntaxErrorclassDynamicRegroupNode(template.Node):def__init__(self,target,parser,expression,var_name):self.target=targetself.expression=template.Variable(expression)self.var_name=var_nameself.parser=parserdefrender(self,context):obj_list=self.target.resolve(context,True)ifobj_list==None:# target variable wasn't found in context; fail silently.context[self.var_name]=[]return''# List of dictionaries in the format:# {'grouper': 'key', 'list': [list of contents]}.""" Try to resolve the filter expression from the template context. If the variable doesn't exist, accept the value that passed to the template tag and convert it to a string """try:exp=self.expression.resolve(context)excepttemplate.VariableDoesNotExist:exp=str(self.expression)filter_exp=self.parser.compile_filter(exp)context[self.var_name]=[{'grouper':key,'list':list(val)}forkey,valingroupby(obj_list,lambdav,f=filter_exp.resolve:f(v,True))]return''@register.tagdefdynamic_regroup(parser,token):firstbits=token.contents.split(None,3)iflen(firstbits)!=4:raiseTemplateSyntaxError("'regroup' tag takes five arguments")target=parser.compile_filter(firstbits[1])iffirstbits[2]!='by':raiseTemplateSyntaxError("second argument to 'regroup' tag must be 'by'")lastbits_reversed=firstbits[3][::-1].split(None,2)iflastbits_reversed[1][::-1]!='as':raiseTemplateSyntaxError("next-to-last argument to 'regroup' tag must"" be 'as'")""" Django expects the value of `expression` to be an attribute available on your objects. The value you pass to the template tag gets converted into a FilterExpression object from the literal. Sometimes we need the attribute to group on to be dynamic. So, instead of converting the value to a FilterExpression here, we're going to pass the value as-is and convert it in the Node. """expression=lastbits_reversed[2][::-1]var_name=lastbits_reversed[0][::-1]""" We also need to hand the parser to the node in order to convert the value for `expression` to a FilterExpression. """returnDynamicRegroupNode(target,parser,expression,var_name)Example:#models.pyclassThing(models.Model):category=models.ForeignKey('Category')#views.pyfromdjango.shortcutsimportrenderfrommy_app.modelsimportThingdefindex(request):things=Thing.objects.all()group_by=request.GET.get('group_by')returnrender(request,'my_template.html',{'things':things,'group_by':group_by})#my_template.html{%dynamic_regroupthingsbygroup_byasregrouped_things%}Withtheregular{%regroup%}tag,"group_by"cannotbeavariable.Enjoy!