Some parts of Django template are kinda widgets1, which are implemented using template tags and there could be a lot of some of them at the page (depending on session cookies for example)
Approach like this will do if their number is limited:
{% extends "some.html" %}
{% load mywidgets %}
{% block widgets %}
{% if widget1 %} {% widget1 %} {% endif %}
{% if widget2 %} {% widget2 %} {% endif %}
...
{% if widgetn %} {% widgetn %} {% endif %}
{% endblock widgets %}
开发者_如何学运维But what if it's not? How to handle a huge amount of widgets?
1 You can see what I mean at iGoogle ;)
I think that these widgets should be some kind of objects, not template tags. Objects that can render them self. Then you can simply store these objects in list and render them in a cycle.
In view you would initialize those objects by passing request
or other required arguments. Widget object would implement __unicode__
method which will render it. Then in template you would render it: {{ widget }}
.
widgets.append( Widget(request) )
--
{% for widget in widgets %}
{{ widget }}
{% endfor %}
Your widgets could even be django models. Then you could store settings and relate them to users.
You might want to check how fein-cms, django-cms implement content placeholders. Any kind of content objects can be associated to placeholder. Content object can render it self, and placeholder just renders collection of them.
精彩评论