I blog in ReSt Part 3

Sun 11 November 2007 by Thejaswi Puthraya

I blog in ReSt Part 3

This is the final part in the "I blog in ReSt" series. This part discusses about how to get the templates going for the blog.

  • Create a folder called templates in the project directory and add the absolute path to the directory in the settings.py TEMPLATE_DIRS
  • Create a file called blog.html in templates
    {% load custom_filters %}
    {% load comments %}
    {% load markup %}
    {% load tagging_tags %}

    {% block title %}Title of the Blog{% endblock %}

    {% block content %}
    {% for blog in blog_object_list %}
      {{ blog.blog_title }}
      {{ blog.blog_file|open_file|restructuredtext }}
      Published on {{ blog.blog_date_pub|date:"F j, Y" }}

    {% get_free_comment_list for blog_app.blog_model blog.id as comment_list %}
    {% get_free_comment_count for blog_app.blog_model blog.id as comment_count %}

    {% if comment_count %}

      {% for comment in comment_list %}
         <div class="comment_{% cycle odd,even %}" id="c{{ comment.id }}">
         <span class="comnum"><a id="c{{ comment.id }}"
         href="#c{{ comment.id }}">#{{ forloop.counter }}</a></span>
         <p><strong>{{ comment.person_name|escape }}</strong> commented,
         on {{ comment.submit_date|date:"F j, Y" }} at {{ comment.submit_date|date:"P" }}:</p>
         {{ comment.comment|restructuredtext }}
         </div>
      {% endfor %}

    {% endif %}

    <p>Post a comment</p>

    {% free_comment_form for blog_app.blog_model blog.id %}

   {% endfor %}

   Viewing page <strong>{{ page }} / {{ pages }}</strong>. Total number of posts are {{ hits }}.

   {% if has_previous %}

       {% if year %}

         {% if month %}
           <p><a href="/blog/{{ year }}/{{ month }}/page/{{ previous }}/">Newer Posts</a></p>
         {% else %}
           <p><a href="/blog/{{ year }}/page/{{ previous }}/">Newer Posts</a></p>
         {% endif %}

       {% else %}

         {% if tag %}
           <p><a href="/blog/tag/{{ tag }}/page/{{ previous }}/">Newer Posts</a></p>
         {% else %}
           <p><a href="/blog/page/{{ previous }}/">Newer Posts</a></p>
         {% endif %}

       {% endif %}

     {% endif %}

   {% if has_next %}

      {% if year %}

        {% if month %}
          <p><a href="/blog/{{ year }}/{{ month }}/page/{{ next }}/">Older Posts</a></p>
        {% else %}
          <p><a href="/blog/{{ year }}/page/{{ next }}/">Older Posts</a></p>
        {% endif %}

      {% else %}

        {% if tag %}
          <p><a href="/blog/tag/{{ tag }}/page/{{ next }}/">Older Posts</a></p>
        {% else %}
          <p><a href="/blog/page/{{ next }}/">Older Posts</a></p>
        {% endif %}

      {% endif %}

    {% endif %}
{% endblock %}

You might be wondering what {% load custom_filters %} is. As the name suggests it is a custom filter to read the contents of the file and return them to the restructuredtext filter.

To write your custom filter

  • Create a new application called mytags and add it to the INSTALLED_APPS in settings.py
  • In the mytags directory create a folder called templatetags which must have an __init__.py file and the python file containing your filter code (here it is custom_filters.py).
from django import template

register = template.Library()

def open_file(value):
"Opens the contents of the file given in value."
    try:
        f = open(value,"r")
        contents = f.read()
    except IOError:
        return None
    f.close()
    return contents

# Register the Filter
register.filter(open_file)

The restructuredtext is a filter from django.contrib.markup. The django.contrib.markup currently supports three markup languages Textile, Markdown and ReSt.

Comments are essential for a blog

Django has an in-built comment system that can be activated almost instantly. For activating comments add the django.contrib.comments to your INSTALLED_APPS in settings.py. There are two types of Comments Systems:

  • Comments: For sites that require only registered users to post comments. This system also has the facility to rate comments based on a concept called Karma Score.
  • FreeComments: For sites that require any user to post comments. This is the type of comments one usually places on a blog. But beware spam will soon catch up. For the people who want to take extra precaution, I would recommend James Bennett's (aka Ubernostrum) Django Comment Utils. Django Comment Utils provides lots of features like email on comment, Akismet based spam filtering, easy moderation etc.

The best part about Django's Comment System is that it is extremely easy to use but its biggest drawback is that it hasn't been documented yet. This is no lame excuse for not using Django Comments. In fact lots of users have blogged on how to use the Comment System. Feel free to search and you'll get hold of them. The one link that explains the comment system in detail would be this.

The rest of the code is regarding pagination. Refer here for more details regarding it.

Now you have a blog written in Django that has all the basic features that Blogger has. Also the blog is highly extensible. Extend the blog and let me know of some cool features that you built into your blog.


I blog in ReSt Part 2

Sat 10 November 2007 by Thejaswi Puthraya

I blog in ReSt Part 2

Once done with setting up the project, we need to code our views and templates to complete the blog. This part deals with coding the views for the blog. The different views that we require are

  • For displaying the landing page
  • For displaying the …
read more

I blog in ReSt Part 1

Fri 09 November 2007 by Thejaswi Puthraya

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 …

read more