Write reusable template part django

In my templates, in many places, I've got repeating parts, like:

       <th class="column-title">
        <a href="?{% query_transform order_by='x' %}">
            {{ objForm.x.label_tag }}</a>
       </th>
       <th class="column-title">
        <a href="?{% query_transform order_by='y' %}">
            {{ objForm.y.label_tag }}</a>
       </th>
       <th class="column-title">
        <a href="?{% query_transform order_by='z' %}">
            {{ objForm.z.label_tag }}</a>
       </th>

Is there a way, to write some "function" to render such html part like: (pseudocode)

in html:

render("x")
render("y")
render("z")

in python:

def render(param):
   return " <th class="column-title">
        <a href="?{% query_transform order_by='+param+' %}">
            {{ objForm'+param+'.label_tag }}</a>
       </th>"

PS. I know, that theoreticaly I can prepare ordered list in view, and then iterate over it in a template, but I think it is not a good solution ,as I prefer to build my view, fields order etc on the template side.

You can use the include template tag to insert your common template code where you need it.

<table>
  <thead>
    <tr>
        {% include "table_header.html" with param=x %}
        {% include "table_header.html" with param=y %}
        {% include "table_header.html" with param=z %}
    <tr>
  </thead>
</table>
Back to Top