I blog in ReStructured Text Part-1
Nowadays everyone blogs. Blogs have become the medium for people to express almost everything. So I started blogging in January on Blogger. Initially I was satisfied with it but started feeling cramped for features. Wordpress and PyBlosxom looked like good alternatives but it was the latter that impressed me the most. I wanted to blog using PyBlosxom. Then I realized, what's the point in knowing Django and still not writing your own blog app.
My requirements for the blog were:
- Markup based: The reasons for choosing markup were
- I have a poor memory and can't recollect the 100's of HTML Tags.
- Check out http://docutils.sourceforge.net/docs/ref/rst/introduction.html#goals
File-System based: Storing all the contents of the blog in a database didn't appeal to me especially when you are restricted by 40 Megs of RAM by your hosting provider (I am not complaining, that's all that I can afford).
Once done with the requirements, I started scripting for my blog and here is a step-by-step process of how to go about it. The blog can be built for any markup langauge like Markdown, PyTextile or ReSt.
- First fire up a django project.
$ django-admin.py startproject myproject
- Then start a new application called blog_app.
$ python manage.py startapp blog_app
- Edit the settings.py to add a database engine and a database of your choice. I used SQLite because it is a lightweight database with a very small footprint. Also add django.contrib.markup, django.contrib.admin, blog_app in the INSTALLED_APPS.
- Open the models.py file in blog_app and create a new Model. The blog will also support tags using Django-Tagging
from django.db import models from tagging.models import Tag from tagging.fields import TagField class Blog_Model(models.Model): blog_title = models.CharField(max_length=250) blog_date_pub = models.DateField() blog_file_name = models.CharField(max_length=250) blog_tags = TagField(max_length=255) def save(self): # Override the save method of Model. super(Blog_Meta, self).save() self.tags = self.blog_tags def _get_tags(self): return Tag.objects.get_for_object(self) def _set_tags(self, blog_tags): Tag.objects.update_tags(self, blog_tags) tags = property(_get_tags, _set_tags) def __unicode__(self): return self.blog_title
- Save the above code and run syncdb from the project directory.
$ python manage.py syncdb
- Uncomment the admin urlpattern from the urls.py file and run the server.
$ python manage.py runserver
- Go to the admin url in your webbrowser and add a few entries to your Blog Models.
In the next part we see how to write the views and templates for the blog.