I bet you have heard the news on the street about this old ericsson programming language coming back to life bringing functional programming on the web. Yes, I talk about erlang! The language that is used by companies like Facebook, Amazon and of course Ericsson for their network products. It offers features like hot code swapping, lightweight inter-process communication and more. Generally it is a really great language that fits really well into the "Cloud computing" industry.

As I was exploring the language a while ago, I came into a project called Apache CouchDB, an object-oriented database, or to be more precise a "document store". At first I thought, oh just another object store, but then, after a bit of research, I got in love with it. It is wonderful because It gives you the ability to store, retrieve and query structured data without having to define a schema, one can also attach files onto each document! And the best of all you can do all of this through a neat web interface! Of course it is programmed in Erlang and this is the reason why it offers great speed, stability and distributed features like replication. As I mentioned earlier you can even query data, using uhm... yes... JavaScript! Smart.

After I used it for a couple of hours, I thought that it would really be a great Django template store! One could serve all templates in CouchDB and also define template instances using documents that include all the variables a template renders. Isn't this some kind of a CMS? I started a simple implementation and after no more than 30 lines of python there it was! Really simple but also really functional!

Here are the steps!

  1. Install CouchDB
  2. Create a new Django project
  3. Create a new app inside tha django project, I called mine totemplate.
  4. Create the appropriate views and setup the urls.py
  5. Design the database on CouchDB
  6. enjoy!

Step 1

Installing CouchDB should be really easy for all platforms.

Mac OS X

You will have to download and install MacPorts (http://www.macports.org/install.php) and then issue the following command:

  1. $ sudo port install couchdb


Here is a link to a tutorial for linux: http://barkingiguana.com/2008/06/28/installing-couchdb-080-on-ubuntu-804.


If you are on windows you can take a look here: http://wiki.apache.org/couchdb/Installing_on_Windows I haven't tested it myself but it should work fine.

After you have installed couchdb, you will also have to install the python library for it. This is called couchdb-python and is available here:
For you that have easy_install installed on your machines, issuing:

  1. $ sudo easy_install couchdb-pytho

should do the job quickly and easilly.

Here is the views.py:

  1. # Create your views here.
  2. from django.http import HttpResponse
  3. from django.template import Template, Context
  4. from couchdb import *
  5. from totemplate.settings import COUCH_SERVER
  7. def show(request, resource_id, page_id):
  8. couch_store = Server(COUCH_SERVER)
  9. category_name = couch_store['indexers'][resource_id]['category']
  10. category = couch_store[category_name][page_id]
  11. template = couch_store['templates'][category['template_id']]
  12. html = Template(template['body']).render(Context(category))
  13. return HttpResponse(html)
  15. def index(request, resource_id):
  16. couch_store = Server(COUCH_SERVER)
  17. indexer = couch_store['indexers'][resource_id]
  18. t = Template(indexer['template'])
  19. html = t.render(Context(couch_store))
  20. return HttpResponse(html)
  22. def resources(request):
  23. couch_store = Server(COUCH_SERVER)
  24. resources = {"indexers":[]}
  25. for indexer in couch_store['indexers']:
  26. if couch_store['indexers'][indexer].has_key('template'):
  27. resources['indexers'] += [indexer]
  28. t = Template( couch_store["settings"]["index"]["body"])
  29. html = t.render(Context(resources))
  30. return HttpResponse(html)
Tagged with:  
Share →
This site is protected by Comment SPAM Wiper.