The title may be a little confusing, but I don’t know how else to call it.
I would like to create a Django project with a large set of applications you could arbitrary turn on or off using INSTALLED_APPS option in settings.py (you would obviously also need to edit urls.py and run syncdb). After being turned on an app should be able to automatically:
-
Register it’s content in site-wide search. Luckily django-haystack has this built-in, so it’s not a problem.
-
Register cron jobs. django-cron does exactly that. Not a problem.
-
Register a widget that should be displayed on the homepage. The homepage should include a list of boxes with widgets form different applications.
I thought about inclusion tags, because you can put them anywhere on a page and they control both content and presentation. The problem is I don’t know how to automatically get a list of inclusion tags provided by my applications, and display them one by one on a homepage. I need a way to register them somehow, and then display all registered tags.
I’m not sure using inclusion tag is actually your best choice… There’s no easy way, AFAIK, to call template tags dynamically from a template (and that’s not the point of a template tag anyway :-).
I suppose I can make the following assumptions on your widgets, correct me if I’m wrong :
requestobject (so it can accessuser,session, etc…)With this, you can think of your widgets as mini-views, returning a string instead of a response:
Now there’s two issues to adress :
First point:
An easy way is to rely on a convention. Have a uniformly named list of functions in a
template_widgets.pymodule in all your applications, eg:Then, you can load a global list of widgets by looking at
INSTALLED_APPS, and have it automatically available in all your templates (using a context processor). Of course, it’s better to load this list lazily to be sure not to waste CPU cycles on building it if you’re not gonna use it.Second point:
Now, you have the list of widgets available, and lazily loaded, in every templates. A convenient syntax for using it would be something like:
But this will not work,
{[widget}}is here a callable, that needs arequestparameter. Djano doesn’t allow you to call callables with parameters from within a template, so you have to modify a bit the context processor to return a list of (lazily) evaluated widget functions.And voilà, the above template code should now be working.
Notes:
INSTALLED_APPSand in eachwidgetslists. Up to you to choose the right ordering method for you (using weighting for example, using a dict to access widgets function by names, etc..)RequestContext.