[Django]-Django Template: Make sections of template reorderable?

2👍

I’d suggest creating a list of the section names in the view corresponding to the order that the user specifies.

def view(request):
    # this list can also be built dynamically based on user preferences
    item_list = ["section_one.html", "section_two.html", "section_three.html"]
    return render_to_response('main_template.html', RequestContext(request, locals()))

Then in the template you can render each section as a sub-template like below, where the sub-templates are named a “NAME.html” format:

{%for item in item_list%}
    {% include item %}

Here’s a reference for the include tag: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include

1👍

It’d be easier to reorder the sections in the view:

The view:

def view(request):
    context = {}
    context['items'] = []

    #decide the order and put the items into the context

    return render_to_response('template.html',context,context_instance=RequestContext(request))

The template:

{%for itemlist in items%}
    {%for item in itemlist%}
        {{item.display_function}}
    {%endfor%}
{%endfor%}

Leave a comment