Brian Ray's BlogPainting is just another way of keeping a diary. --Picasso
Sun, 15 Jan 2006
Django Powered
So, I signed my first client. Part of job is to add dynamic content to portions of a large commercial (mostly static) site. After a closer look, I realized the base content can be templated with Django's template system. Then I wrote a catch-all view and locate the page's old url from the model. So besides the headers and footers, the old content is now pulled in from the database itself. This was preferable because I was able to also give some versioning tools to the content itself (although static blocks). So my setup looks something like this: Framework Configure Render +------------+-----------+---------------------+ | v | v | | |Apache2 |httpd.conf | Branding Logo | |mod_python | v | | | v |settings.py| Navigation / Search | |Python2.4 | v | | | v |url.py | Navigation Crumbs | |Django | v | <------ (later: Login bar here) |SQLObject |view.py | Content (cached) | |psycopg | v | | |PostgreSQL |model.py | Footer | +------------+-----------+---------------------+ This is the view:
def base(request,template="sample/base"):
if request.path == "/" or request.path == "index.html":
sp = get_object_or_404(staticpages, relative_page_url__exact="./index/")
else:
sp = get_object_or_404(staticpages, relative_page_url__exact=".%s" % request.path.replace("index.html",""))
# low level cache
key = "sample_static_%s_%s" % ( sp.id, sp.revision)
content = cache.get(key)
if content == None:
content = sp.content
cache.set(key, content, 30000)
return render_to_response(template, {
'title':sp.page_title,
'meta':sp.meta,
'js':javascript(request),
'searchbar':searchbar(request),
'crumbsbar':crumbsbar(request),
'content':content,
})
Also, having the content in the database allowed me easily to re-write the search engine. Good URL's do not change so I have things like "specifics/list.html" in the database. For migration, I wrote a quick script to recursively grab the content from the static site and put it in PostgreSQL table created with Django. The low-level cache (See the cache.get() method above) I added to speed up for these static pieces. I am so amazed how much faster these pages are being served. Even though they are now being pulled in from a database they are now faster than when they were being served as static content from a large web hosting agency. This is true with and without the cache. In concept the cache should be faster. Although I am having a hard time detecting the difference. I tried "file" and "memcache" methods. I am afraid this site is not the sort to be slashdoted, so I guess I need not concern myself too much. btw, I used a Python framework called funkload to conduct load tests on Django. Seems fairly cool but a little bit overkill for what I need to test. Maybe simple webunit (I did not try) would have been easier. Django is fast. Fast is setting up. Fast for fitting in with legacy item. And these pages load really fast. Not only faster than most JAVA and RoR sites, but faster than many static sites.
|
Categories Calendar
Check out my new Blog -> |
|||||||||||||||||||||||||||||||||||||||||||||||||