Using Diazo in Django
Door Douwe van der Meij
This blog post covers the integration of Diazo in Django with examples.
In my previous blog post (Dutch) I explained what Diazo is about. For those who can't read Dutch, the official Diazo website explains it a little bit more technical or check my Slideshare presentation. In this post my colleague talked about a generic interface to create themes, based on the theme editor of Plone. Diazo was originally developed in the Plone community.
The first thing to do to get Diazo working in Django is setting up an intermediate package calledDjango-Diazo.
In Django-Diazo we created a WSGI-wrapper what will be the entry point for Diazo and what will take care of selecting the right assets for showing the theme selected in Django.
Django-Diazo comes with an Admin interface and a management command called "syncthemes". Furthermore it is possible to completely omit the Django template engine and directly feed the context-variable of the Django-views to Django-Diazo. The internals of the package will convert the context-variable, which is a dictionary, to XML and will pass it as content into Diazo. To illustrate the technique, we created an example.
When developing a Django application, for me, creating the models and defining logic is most fun. When it comes to the web interface, Django Admin helps a lot, but is in most cases not sufficient for my needs. Also, front-end development nowadays is more a profession on its own, I’m a back-end developer and certainly not a designer (of front-ends ;)).
When someone hands over a html/css/js theme (fully functional in “all” browsers…), I prefer to leave this code as is and only fill the parts that should be content. This is contrary to the way Django tells us how to create templates. I.e., you have to strip the html, cut it into different parts and provide Django template-tags to insert data and/or logic.
We already know Diazo covers this problem. When using Diazo to insert content in existing html templates, the Django template engine becomes superfluous (sorry guys) and outputting the context just as XML that can be interpreted by Diazo (by default) is sufficient. Reducing this to its essence, we only define the context of the Django views and the rules to replace elements of the theme with actual content.