[Fixed]-Reusable HTML component libraries in Django

16👍

Have you taken a look at inclusion tags? https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags

I believe that these would do exactly what you want, you can still specify parameters but you can just pass along a template to have it render really easily.

Example:

from django import template
register = template.Library()

@register.inclusion_tag('list_results.html')
def list_results(results):
    return {
        'results': results,
        'count': len(results),
    }

list_results.html:

Found {{ count }} results.<br>
<ul>
{% for result in results %}
    <li>{{ result }}</li>
{% endfor %}
</ul>
👤Wolph

0👍

Use templatetags and html template… Then you can reuse that HTML template…

👤Nilesh

Leave a comment