Created a front page app
Posted in Django
on November 10th, 2009 by
Stephen DeGrace
I decided to encapsulate the functionality of the front page with its own separate app. There is only one visible change to the way the front page looks and acts, but it is implemented in an entirely different way.
Previously the way I did this was with the Page application. Page includes a template which uses a special template tag to render certain content as a Django template in the same context. I used this to write a particular page with template content which created the dynamic content you see (the Latest Article and Latest Gallery blurbs). TinyMCE is not really suitable for writing a good template, so I handled this one page through the database admin. Since it was implemented with a Page, the <title>Title</title> and the page top heading had to be the same.
I wanted to have a different title for the browser window and for the main page heading for this one page, but I didn't want to change the whole page application, because for every other page I like that functionality, and I didn't want to kludge it further for this one-off. So I decided instead to encapsulate this functionality in a special application. This could have been as simple as a minimal view and a hard-coded template, but I decided to try something more ambitious.
I've commented on the difficulty in creating administratively-controlled widgetry in Django before, and I noted that this kind of functionality could perhaps be instantiated by having a special app that polls other installed apps for "widgets" they want to make available and provides template tags for publishable positions and a database table to remember what goes where. For my Home application, I decided to try out one aspect of this functionality, the polling of the installed apps.
The Home app keeps a list of functions it calls, passing the request, and displaying the return value every time someone requests the root page (or really, the URL that is set up to point to it). Any app which wishes to publish content on the front page defines a file called homeviews.py which defines all of the functions it wants called, and which registers these functions with the Home app. They can also suggest a relative ordering.
The Home app itself has a setting for the browser window title and allows total control of other general content. This overall setup lets every app define within itself what it wants to display on the front page as a kind of one-off widget which is dynamically generated.
So, if I ever wanted as an exercise to duplicate the ability of applications like Wordpress to make widgets available which can be programmed, placed in a variety of locations and removed without editing files, I have a sort of proof of concept for one part of it.
Comments:
There are 0 comments on this item. Be the first to comment.
Post a Comment
* Required field, your email will not be posted.