Take the 2-minute tour ×

When using Django templates, should I have some templates that act like "subroutines", so to speak, or should I generate HTML from within my code in these cases?

For example, I have a template with several lists of names, each of which I want to turn into a select. Should I have a template that renders the name_list variable into a select, and do something like this:

#in the view:
return {'name_list_1': name_list_1, 
        'name_list_2': name_list_2, 
        'name_list_3': name_list_3}

#in the template:
{% with name_list_1 as name_list %}
  {% include "sub_name_list_select.html" %}
{% endwith %}
{% with name_list_2 as name_list %}
  {% include "sub_name_list_select.html" %}
{% endwith %}
{% with name_list_3 as name_list %}
  {% include "sub_name_list_select.html" %}
{% endwith %}

Or should I have a function in my code, name_list_to_select_html, which does the same job, and do this:

return {'name_list_1_html': name_list_to_select_html(name_list_1), 
        'name_list_2_html': name_list_to_select_html(name_list_2), 
        'name_list_3_html': name_list_to_select_html(name_list_3)}

#in the template:
{{ name_list_1_html|safe }}
{{ name_list_2_html|safe }}
{{ name_list_3_html|safe }}

Or are both of these wrong and I am getting the philosophy totally wrong?

Additional question: in terms of speed, is it slow to constantly include templates? Is that a bonus point for the in-code html generation?

share|improve this question
stackoverflow.com/faq#dontask. This might be more appropriate some place like codereview.stackexchange.com –  Chris Pratt Jul 19 '12 at 20:26
which particular criteria does this question fall under? it seems pretty straightforward to me: how do django developers accomplish this task? –  Claudiu Jul 19 '12 at 21:20
It's a subjective question. The bulleted list merely provides examples of common subjective questions, but if you're asking what people recommend, your question is subjective and not appropriate for StackOverflow. Like I said, the Code Review site would be okay because the entire nature of that site is to gain subjective opinions. –  Chris Pratt Jul 19 '12 at 21:29
@ChrisPratt: ah ok, i think you're right. voting to close the question. –  Claudiu Jul 20 '12 at 3:12
add comment

1 Answer

Generally, HTML should only be generated in the templating system or directly related code. That keeps the view of the data completely separate from the business logic. Go with your first solution.

As for performance, Django should probably take around the same amount of time running either code. But it has built-in view and template fragment caching if you know those segments of code don't need to be regenerated on every request.

share|improve this answer
is my first solution the commonly-accepted way to pass variables to templates, or is there something more elegant? something irks me about not being able to call a template like a function. –  Claudiu Jul 19 '12 at 18:31
also, due to the limited nature of django templating, i am invariably forced to have the view of the data interfere with the python code, as i have to return data structures amenable to the templating system. for example, if i want to pass a dict, it's hard to get the templating system to sort it, so i end up having to turn it into a list of items and do the sorting on the view code. isn't having the views in views.py enough separation? –  Claudiu Jul 19 '12 at 18:33
Views gather the data to present (in an easy-to-template way), but I believe all HTML should be done directly at the template level. When I require more python functionality to write HTML, I create custom tags and filters. You also have the option of using a different templating system (although I've never tried it myself). –  Matt S Jul 19 '12 at 18:47
add comment

Your Answer


