Matthew Daly's Blog

I'm a web developer in Norfolk. This is my blog...

29th March 2012 9:29 pm

Yet Another Tutorial for Building a Blog Using Python and Django - Part 4

Welcome back! In this tutorial we’ll continue extending our Django-powered blogging engine. We’ll add the capability to assign blog posts to categories, and comment on posts. We’ll also generate an RSS feed for our blog posts.

Categories are somewhat tougher to implement than most of what we’ve done beforehand. One category can be assigned to many blog posts, and many categories can be assigned to one blog post, so this relationship is described as a “many to many relationship” when drawing up the database structure. What it means is that you can’t directly map categories onto posts and vice versa - you have to create an intermediate database table for the relationship between posts and categories.

Here’s what your models.py should look like:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=40, unique=True)
description = models.TextField()
class Meta:
verbose_name_plural = "Categories"
def __unicode__(self):
return self.title
def get_absolute_url(self):
return "/categories/%s/" % self.slug
class Post(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField()
text = models.TextField()
slug = models.SlugField(max_length=40, unique=True)
author = models.ForeignKey(User)
categories = models.ManyToManyField(Category, blank=True, null=True, through='CategoryToPost')
def __unicode__(self):
return self.title
def get_absolute_url(self):
return "/%s/%s/%s/" % (self.pub_date.year, self.pub_date.month, self.slug)
class CategoryToPost(models.Model):
post = models.ForeignKey(Post)
category = models.ForeignKey(Category)

We’re adding quite a bit of new code here. First of all we’re defining a new model called Category. Each category has a title, a description, and a slug (so we can have a dedicated page for each category). As usual, we define methods for unicode and get_absolute_url, but also note the class Meta. Here we’re defining some metadata for the class (ie, data about the data). The only thing we do here is essentially telling the admin interface that the plural of Category is not “Categorys” but “Categories”.

Then, in Post we add an additional field called Category, which we define as a ManyToManyField. Note the parameters passed through - we’re saying here that a post need not be assigned a category, and that CategoryToPost should be used as an intermediate table to link posts to categories.

Finally, we define the aforementioned CategoryToPost model, which has two fields, post and category. Both of these are foreign keys, mapping to a blog post and a category respectively. By creating entries in this table, a link can be created between a post and a category.

With our model changed, it’s time to update our admin.py as well:

import models
from django.contrib import admin
from django.contrib.auth.models import User
class CategoryAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
class CategoryToPostInline(admin.TabularInline):
model = models.CategoryToPost
extra = 1
class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
exclude = ('author',)
inlines = [CategoryToPostInline]
def save_model(self, request, obj, form, change):
obj.author = request.user
obj.save()
admin.site.register(models.Post, PostAdmin)
admin.site.register(models.Category, CategoryAdmin)

Here we define a new class called CategoryAdmin, which details how we’re changing the admin interface for Category from the defaults generated from the fields provided. The only change we make here is that we prepopulate the slug field from the title, much like we did with blog posts.

Next, we define an inline for the relationships between categories and post, called CategoryToPostInline. This is a new concept - essentially it means that the category to post relationships can be defined in another model’s admin interface. We define the model this applies to, and that by default we will only add one additional field for adding categories when writing or editing a post (though users can add as many as they wish, or none). Note that the model this is based on is admin.TabularInline - this represents a tabular layout. If you prefer, you can use an alternative layout by using StackedInline instead.

Then, in PostAdmin we add our newly declared CategoryToPostInline to the PostAdmin class as an inline. Finally, at the bottom we register Category with the admin interface, so we can create and manage categories easily.

With that done, it’s time to edit our views.py:

# Create your views here.
from django.shortcuts import render_to_response
from django.core.paginator import Paginator, EmptyPage
from blogengine.models import Post, Category
def getPosts(request, selected_page=1):
# Get all blog posts
posts = Post.objects.all().order_by('-pub_date')
# Add pagination
pages = Paginator(posts, 5)
# Get the specified page
try:
returned_page = pages.page(selected_page)
except EmptyPage:
returned_page = pages.page(pages.num_pages)
# Display all the posts
return render_to_response('posts.html', { 'posts':returned_page.object_list, 'page':returned_page})
def getPost(request, postSlug):
# Get specified post
post = Post.objects.filter(slug=postSlug)
# Display specified post
return render_to_response('single.html', { 'posts':post})
def getCategory(request, categorySlug, selected_page=1):
# Get specified category
posts = Post.objects.all().order_by('-pub_date')
category_posts = []
for post in posts:
if post.categories.filter(slug=categorySlug):
category_posts.append(post)
# Add pagination
pages = Paginator(category_posts, 5)
# Get the category
category = Category.objects.filter(slug=categorySlug)[0]
# Get the specified page
try:
returned_page = pages.page(selected_page)
except EmptyPage:
returned_page = pages.page(pages.num_pages)
# Display all the posts
return render_to_response('category.html', { 'posts': returned_page.object_list, 'page': returned_page, 'category': category})

Here we import the Category model as well as the Post model. Then, the only additional change we need to make is to add a brand new getCategory view function. Note that this is quite similar to the getPosts function - we set up pagination in the same way, and rather than get all the posts, we get just those in the specified category. Also note that we’re using the template category.html rather than posts.html here, and we pass through category as well as posts and page when we return the render_to_response.

The next change we need to make is adding category.html. Go into your template directory and save the code below as category.html:

{% include 'header.html' %}
<h1>Posts for {{ category.title }}</h1>
{% if posts %}
{% for post in posts %}
<h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
{{ post.text }}
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
{% else %}
<p>No posts matched</p>
{% endif %}
{% include 'footer.html' %}

With our template in place, the last step is to add an appropriate URLconf. Edit urls.py to look like this:

from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'blog.views.home', name='home'),
# url(r'^blog/', include('blog.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# Home page
url(r'^$', 'blogengine.views.getPosts'),
url(r'^(?P<selected_page>\d+)/?$', 'blogengine.views.getPosts'),
# Blog posts
url(r'^\d{4}/\d{1,2}/(?P[-a-zA-Z0-9]+)/?$', 'blogengine.views.getPost'),
# Categories
url(r'^categories/(?P<categorySlug>\w+)/?$', 'blogengine.views.getCategory'),
url(r'^categories/(?P<categorySlug>\w+)/(?P<selected_page>\d+)/?$', 'blogengine.views.getCategory'),
# Flat pages
url(r'', include('django.contrib.flatpages.urls')),
)

Now, if you run python manage.py syncdb again, the category system should be up and running.

The next step is to add the facility to handle comments. Again, Django has its own application built in for handling comments, so go into settings.py and enter the following under INSTALLED_APPS:

     'django.contrib.comments',

Then run python manage.py syncdb again to generate the appropriate database tables. You’ll also need to amend urls.py to provide a dedicated URL for comments:

# Comments
url(r'^comments/', include('django.contrib.comments.urls')),

Place this before the URLconf for the flat pages.

Comments can be attached to any type of content, but we only want to attach them to blog posts, and they should only be visible in the single post template. But first of all, let’s add a comment count to posts in posts.html and category.html. Replace posts.html with this:

{% include 'header.html' %}
{% load comments %}
{% if posts %}
{% for post in posts %}
<h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
{{ post.text }}
{% get_comment_count for post as comment_count %}
<h3>Comments: {{ comment_count }}</h3>
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
{% else %}
<p>No posts matched</p>
{% endif %}
{% include 'footer.html' %}

And replace category.html with this:

{% include 'header.html' %}
{% load comments %}
<h1>Posts for {{ category.title }}</h1>
{% if posts %}
{% for post in posts %}
<h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
{{ post.text }}
{% get_comment_count for post as comment_count %}
<h3>Comments: {{ comment_count }}</h3>
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
{% else %}
<p>No posts matched</p>
{% endif %}
{% include 'footer.html' %}

The only significant changes here are that at the top we load comments, and underneath the post text we get the comment count for each post as the variable comment_count, then we display it underneath.

Now, we want to go further with our single post template. As well as a comment count, we want to add the actual comments themselves. Finally, we need a form for adding comments - in theory you can use the admin interface for doing this, but it’s very unlikely you’d want to do so. Open up single.html and edit it to look like this:

{% include 'header.html' %}
{% load comments %}
{% for post in posts %}
<h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
<h3>{{ post.pub_date }}</h3>
{{ post.text }}
<h3>By {{ post.author.first_name }} {{ post.author.last_name }}</h3>
<h3>Categories: {% for category in post.categories.all %} {{ category.title }} {% endfor %}</h3>
{% get_comment_count for post as comment_count %}
<h3>Comments: {{ comment_count }}</h3>
<ol>
{% get_comment_list for post as comments %}
{% for comment in comments %}
<li>{{ comment }}</li>
{% endfor %}
</ol>
{% render_comment_form for post %}
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
{% include 'footer.html' %}

This includes the same changes as the other two templates, so we load comments and display the comment count. Afterwards, we get the comment list for this post as comments, and then loop through the comments, showing them in an ordered list. Afterwards, we then use render_comment_form to show the default comment form for this post. If you’d prefer to create your own comment form, you can use get_comment_form instead to get a form object you can use in the template.

You’ll also need to make some minor changes to the view to get the form working. Save single.html and open blogengine/views.py and add the following line of code to your import statements:

from django.template import RequestContext

Then, amend the final line of the getPost function as follows:

    return render_to_response('single.html', { 'posts':post}, context_instance=RequestContext(request))

The reason this needs to be changed is that the comment form includes the {% csrf_token %} tag, which requires information from the request object, and in order to do so rather than the default context, you need to pass through a RequestContext object instead, but don’t worry too much about the details.

If you now ensure the development server is running and visit a blog post, you should now see that you can post comments. If you want to enhance this very basic comment form, take a look at the excellent documentation on the Django website. Alternatively, there are a number of third-party comment services, such as Disqus and IntenseDebate that can handle comments for you and just require you to paste a snippet of code into whatever template you want to enable comments on, and these may be more convenient.

Finally for this lesson, as promised, we’ll implement our RSS feed. Again, there’s an application bundled with Django that will do this - the syndication framework. Open settings.py and paste the following line in at the bottom of your INSTALLED_APPS:

     'django.contrib.syndication',

Save the file and run python manage.py syncdb to add the appropriate tables to your database. Then, we need to add a URLconf for the RSS feed. We’ll allow a consistent naming scheme for RSS feeds, so this will be /feeds/posts, and if you wanted to you could add /feeds/comments, for instance. Add this to you urls.py, before the url for flat pages:

# RSS feeds
url(r'^feeds/posts/$', PostsFeed()),

We’ll also need to tell urls.py where to find PostsFeed(). In this case, we’re going to put it in the view, so add this import line near the top:

from blogengine.views import PostsFeed

Now open blogengine/views.py and add the following line to the import statements at the top:

from django.contrib.syndication.views import Feed

Then add the following class declaration to the bottom:

class PostsFeed(Feed):
title = "My Django Blog posts"
link = "feeds/posts/"
description = "Posts from My Django Blog"
def items(self):
return Post.objects.order_by('-pub_date')[:5]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.text

This is pretty simple. We import the Feed class from thew views provided by the syndication framework, then we base PostsFeed on Feed. We set the title, the link for the feed, and a description for the feed. Then we get the last 5 Post objects in reverse chronological order, and we define each item’s title as the post title. and each item’s description as the text of the post. From here’ it’s pretty easy to see how you could create feeds based on comments, or pretty much any other object that might exist in the database.

And with that done, our blogging engine is pretty-much feature-complete. We have blog posts with comments, categories, an RSS feed, and flat pages, but the look and feel of the site definitely needs some attention. Next time, we’ll make our blogging engine look a little nicer. Once again, the code is available on GitHub in case you find that more convenient.

24th March 2012 6:23 pm

Yet Another Tutorial for Building a Blog Using Python and Django - Part 3

Welcome back! In this instalment, we’ll make some changes to our URL structure for blog posts, we’ll add support for multiple authors and static pages, and we’ll add some more templates.

First of all, our URL structure. The existing structure works fine, but it would be better if we included a representation of the date of publication. If you’re familiar with WordPress, you’ll know it offers several different URL forms, one of which is the post name alone as we’re using here, and another of which is the year, month and name. We’ll use the latter of these URL schemes with our blogging engine.

This seems like a good opportunity to introduce the interactive Python shell that comes with Django. Make sure you have a few dummy posts set up, then in the project directory (DjangoBlog/, not the top-level one but the one inside that), enter the following command:

python manage.py shell

This will start up an interactive Python shell which you can use to interact with your Post objects. Now, the first step is to import your Post model:

>>> from blogengine.models import Post

We now have access to our Post objects – let’s take a look at them:

>>> Post.objects.all()
[<Post: My first blog post>, <Post: My second blog post>, <Post: My third post>, <Post: My fourth post>, <Post: My fifth post>, <Post: My sixth post>]

You may have completely different post objects, or a different number of them, but that’s fine. Remember we set __unicode__(self) to return self.title? Here we see that each blog post is represented by its title. Now let’s get one of our Post objects:

>>> p = Post.objects.get(pk=1)
>>> p
<Post: My first blog post>

In the first line above, we get the Post object with the primary key of 1, and store a reference to it as p. We then demonstrate that it is, indeed, one of our blog posts by outputting its title.

If you’re not familiar with relational database theory, a primary key is a value in a database table that refers uniquely to one entry in the table, so that if you refer to an entry by its primary key, you can be sure you’re getting the correct value. By default, Django models generate a field called id in addition to the ones you define, which is set as the primary key, and this is set to auto-increment, so for instance, every time you add an additional blog post, it gets the next number as its id. Here, we just want to get access to a single blog post object, so we just enter 1 as the primary key in order to get the earliest blog post.

Next, we get the publication date:

>>> p.pub_date
datetime.datetime(2012, 3, 19, 12, 11, 10)

This returns a datetime.datetime object. If you look at the documentation for Python’s datetime module, you’ll notice that it has attributes called day, month and year. Here’s how we can use these to get the information we want:

>>> p.pub_date.month
3
>>> p.pub_date.day
19
>>> p.pub_date.month
3
>>> p.pub_date.year
2012

It’s that simple – we just refer to the attribute we want to retrieve. So, it should now be pretty easy to understand how we can get the date for each blog post.

Exit your Python shell with Ctrl-D and head back into the blogengine/ folder. Then open models.py in your text editor and add the following method to the bottom of your Post class:

def get_absolute_url(self):
return "/%s/%s/%s/" % (self.pub_date.year, self.pub_date.month, self.slug)

Now, you haven’t seen get_absolute_url before. Every time you create a model in Django, you should really create a get_absolute_url method for it. In essence, it defines a single, canonical URL for that object, whether it’s a blog post, a user, or what have you. By creating one method that defines the structure for the URL for this type of object and referring to it elsewhere, we only need to change it in one place if we want to make any changes to how we determine the URL for that type of object.

What we do here is we define the URL as being /year/month/slug/. If you want, you can quite easily make it include the day as well like this:

def get_absolute_url(self):
return "/%s/%s/%s/%s/" % (self.pub_date.year, self.pub_date.month, self.pub_date.day, self.slug)

With our model updated, let’s change our URLconf accordingly. Return to the inner DjangoBlog/ directory and open up urls.py, then amend the lines for the blog posts as follows:

# Blog posts
url(r'^\d{4}/\d{1,2}/(?P<postSlug>[-a-zA-Z0-9]+)/?$', 'blogengine.views.getPost'),

What we’ve changed here is that we’ve told urls.py to expect blog posts that look like 4 digits, then a forward slash, then one or two digits, then another forward slash, then a slug that can include hyphens, upper or lower case letters, and numbers.

With that done, we just need to update our template. Edit templates/posts.html to look like this:

<html>
<head>
<title>My Django Blog</title>
</head>
<body>
{% for post in posts %}
<h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
<h3>{{ post.pub_date }}</h3>
{{ post.text }}
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
</body>
</html>

Literally all we do is replace post.slug with post.get_absolute_url and remove the leading forward slash. If you then run python manage.py syncdb, restart the development server and go clicking around your posts, you should be able to see that our new URL system is now up and running.

With that done, the next step is to add support for multiple authors. Now, you might think that we’re going to have to create a new model for users, but that’s not so – Django ships with a number of useful models already, and we’re going to use one of them here.

Now, first of all, we need to amend our Post model to include the author’s details. Edit your blogengine/models.py to look like this:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField()
text = models.TextField()
slug = models.SlugField(max_length=40, unique=True)
author = models.ForeignKey(User)
def __unicode__(self):
return self.title
def get_absolute_url(self):
return "%s/%s/%s/" % (self.pub_date.year, self.pub_date.month, self.slug)

There are two significant changes here. First, we import User from django.contrib.auth.models. User is a model provided by the auth model, and we’re going to use it here to represent the author of a given post. Then in the class definition of Post, we add a new field called author.

Note here that author is a foreign key field, and is passed a User object. Again for those unfamiliar with relational databases, a foreign key is a field in a database table that is also a primary key in another database table. Here we’re declaring that the author is one of the entries in the User table.

As well as this, we need to make some changes to the admin interface. By default, when we make a field in a model a foreign key, the admin interface will show a dropdown list of all of the instances of that object (so here, it would be a list of all the users on the system). But we don’t want that. We want the author to automatically be set as the current user, and for there to be no way to override this.

Open up admin.py and change it to look like this:

import models
from django.contrib import admin
from django.contrib.auth.models import User
class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
exclude = ('author',)
def save_model(self, request, obj, form, change):
obj.author = request.user
obj.save()
admin.site.register(models.Post, PostAdmin)

The changes made here are simple. We import the User model, and in the PostAdmin class definition we exclude the author field – this means that we don’t show this field at all.

Note the addition of the save_model method. In Django it’s easy to create a new object using your models:

>>> from blogengine.models import Post
>>> p = Post()
>>> p
<Post: >
>>> p.title="My new blog post"
>>> p
<Post: My new blog post>

However, the new object won’t actually be stored in the database properly until you call the save() method. Here, you would need to enter p.save() (Note this won’t actually work unless you enter all the fields manually). What we’re doing in admin.py is overriding the default save() method to set the author to the name of the user who wrote the post.

Now run python manage.py syncdb again. Note that as you’ve changed the Post model, your existing posts will be lacking the required author field, and so you may need to add these again manually – this will be a numeric ID mapping to a user id. If you only have one user set up, you should just be able to set this to 1 by using an UPDATE SQL query.

If you now make sure the development server is running and log into the administrative interface, the first page you see should have a section marked “Auth”, with two items underneath named Groups and Users.

Now, cast your mind back to when you activated the admin interface and synced the database. If you recall, at this time you were asked to create a superuser account in order to log into the admin interface. This was actually provided by the django.contrib.auth application, one of the applications that are shipped with Django and are active by default. This contains the User and Group models.

If you’re familiar with Linux or Unix, the Auth application will feel very familiar. The account you created at the start was a superuser account, much like the root account on a Unix system, with unlimited privileges. Other users can be created, and given permissions on an individual basis. You can also create groups and add users to those groups, and then set the privileges for those groups en masse. For instance, in a large collaborative blog with many authors, you may have one group for people who contribute articles who can create new posts, editors who can edit existing posts and so on. Similarly, if you had a working comments system, you could easily set up a moderators group who can delete comments, and add people to that group.

Let’s create another user account so we have more than one. From the main admin page, click on the link for Add next to Users. You’ll be taken to a screen that prompts you for a username and password for the new user. Fill these in (there are two password fields for confirmation purposes) as you wish – here I’m setting the new user as bob. On the next screen you can add some additional details for the new user account, such as first name, last name and email address – do this so you have some information to work with.

Lower down you’ll see a dialogue for entering the permissions. You can make the new user a superuser so that they have permission to do anything, you can say whether or not they are staff (they need to be staff to use the admin interface, so check that), and whether they are active (making it easy to deactivate a user account without the need to delete it). Below you’ll see another dialogue showing the available permissions and allowing you to allocate them to that user. Further down, you’ll see a dialogue for changing the start date and last login date for the user, and finally a dialogue for adding new groups and adding the user to existing groups.

Save the user details once you’re done, then go into your superuser account and add a first and last name so we have some data to work with for that as well. Note that just as with a root account on a Unix box, it’s not a great idea to use a superuser account for everyday work (you should create an account that has the minimum privileges you need and use that), but we’ll stick with it for now just for learning purposes – don’t forget if you should roll out a public facing Django-powered site in future, though!

With that done, we now have some data to work with to identify the author of a given post. Let’s fire up the interactive shell again with python manage.py shell:

>>> from blogengine.models import Post
>>> Post.objects.all()
[<Post: My first post>]
>>> p = Post.objects.get()
>>> p
<Post: My first post>
>>> p.author
<User: root>
>>> p.author.first_name
u'Matthew'
>>> p.author.last_name
u'Daly

Here, we can see that it’s easy to get the author’s details from the post. We define p as a reference to the single Post object,then we get the author, which in this case is called root. As I’ve defined a first and last name, we can get those too with p.author.first_name and p.author.last_name, which are strings containing the first and last name respectively. Note the ‘u’ before the string – this just indicates that the string is Unicode.

So from here, it’s pretty easy to display the author’s name in each post. Go into templates/posts.html and add the following line where you want your author details to appear:

        <h3>By {{ post.author.first_name }} {{ post.author.last_name }}</h3>

Now, as long as you’ve added a first name and last name to that author’s details, if you visit http://127.0.0.1:8000, you should see the appropriate details.

Our next step is to add the facility to create flat pages, somewhat like the Pages functionality in WordPress. Again, Django comes with an application that will handle this, called flatpages, but it’s not enabled by default. Go into DjangoBlog/settings.py and at the bottom of INSTALLED_APPS, add the following:

    'django.contrib.flatpages',

Then run python manage.py syncdb again to add the appropriate tables to your database. Now, we need to add a flat page. Go back to the main page of the admin interface, and you should see that you now have the facility to add flat pages. Click on the Add link for flat pages, and give your page a URL, a title, and some text (here I’m giving it a URL of /about/ and a title of About), add it to a site at the bottom (this will say example.com, but don’t worry about that, it’s to do with the Sites application, which we’re not looking at right now) then save it. You should now have a FlatPage object available.

Let’s take a look at the tables created for flatpages using the sqlall command:

python manage.py sqlall flatpages
BEGIN;
CREATE TABLE "django_flatpage_sites" (
"id" integer NOT NULL PRIMARY KEY,
"flatpage_id" integer NOT NULL,
"site_id" integer NOT NULL REFERENCES "django_site" ("id"),
UNIQUE ("flatpage_id", "site_id")
)
;
CREATE TABLE "django_flatpage" (
"id" integer NOT NULL PRIMARY KEY,
"url" varchar(100) NOT NULL,
"title" varchar(200) NOT NULL,
"content" text NOT NULL,
"enable_comments" bool NOT NULL,
"template_name" varchar(70) NOT NULL,
"registration_required" bool NOT NULL
)
;
CREATE INDEX "django_flatpage_a4b49ab" ON "django_flatpage" ("url");
COMMIT;

So we can see that django_flatpage, which contains the details about the actual flat pages, has the fields id, url, title, content, enable_comments, template_name, and registration_required. Some of these options are under the advanced options in the flat page interface, so you may have missed them. Now fire up python manage.py shell again:

>>> from django.contrib.flatpages.models import FlatPage
>>> f = FlatPage.objects.get()
>>> f
<FlatPage: /about/ -- About>

Here we have one FlatPage object only (note that get() should only be used if you will only get one result back), which is represented by a string that includes the URL and title.

>>> f.content
u'This is my about page.'

We can easily access any of the fields in the flat page. Now, we need to define some URLs for our flat pages. Exit the Python shell and open urls.py, then insert the following rule underneath the one for blog posts:

# Flat pages
url(r'', include('django.contrib.flatpages.urls')),

Note that this must be the last rule in your urls.py, because it will match anything. Now, you can try and load /about/, or whatever page you’ve created, but you’ll get an error stating that the template does not exist, so we need to create that. Go into your template directory, and create a directory inside that called flatpages. Then create a new file in there called default.html, and add the following code to it:

<html>
<head>
<title>My Django Blog</title>
</head>
<body>
<h1>{{ flatpage.title }}</h1>
{{ flatpage.content }}
</body>
</html>

Now, make sure you have the development server running and try to load http://127.0.0.1:8000/about/, or whatever your flat page URL is, and you should see your flat page’s title and content.

One final task for this lesson – we’re going to refactor our templates a little so that as little code as possible is duplicated and if we want to change anything we need to only do so in one place. Go to your template directory and edit posts.html to look like this:

{% include 'header.html' %}
{% for post in posts %}
<h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
<h3>{{ post.pub_date }}</h3>
{{ post.text }}
<h3>By {{ post.author.first_name }} {{ post.author.last_name }}</h3>
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
{% include 'footer.html' %}

Here we’re taking the header and footer of the page out and replacing them with code that includes another file there instead. Next, we need to create those files in the same directory. Here’s header.html:

<html>
<head>
<title>My Django Blog</title>
</head>
<body>

And here’s footer.html:

</body>
</html>

None of this is terribly complex – we’re just moving the code into another file so that other templates can use the same files. Now save a copy of posts.html as single.html – we’re going to create a template for a single blog post. Edit the original posts.html to look like this:

{% include 'header.html' %}
{% for post in posts %}
<h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
{{ post.text }}
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
{% include 'footer.html' %}

We’re just removing the date and author details from the template that shows multiple posts. Our existing single.html file can remain as it is for now, since that still has all the additional information we want to include in an individual post.

While we’re here, let’s update our flat pages to use the same header and footer. Go into the flatpages directory and change default.html to look like this:

{% include 'header.html' %}
<h1>{{ flatpage.title }}</h1>
{{ flatpage.content }}
{% include 'footer.html' %}

Note that the path to the template files is not relative to flatpages/default.html, but relative to the root of the template directory.

The last thing to do is to amend the view for our blog to use the correct templates. Go into blogengine/views.py and change the getPost (NOT getPosts) function to pass the single.html template to render_to_response, instead of the posts.html template:

# Display specified post
return render_to_response('single.html', { 'posts':post})

You should now notice that the single posts and multiple posts are using different templates.

Hope you’ve enjoyed this lesson, and I’ll do another one as soon as I can. The code is available on GitHub if you prefer to get it that way.

19th March 2012 3:18 pm

Yet Another Tutorial for Building a Blog Using Python and Django – Part 2

In the first part of this tutorial, we got the core elements of our blogging application working - we set up our model for posts, and a view, template and URL configuration to view the index. Next we’ll start extending this very basic functionality - we’ll add a view for individual posts as well, and we’ll allow for each post to have a separate URL.

First, we need to set up some pagination for the home page. At this point, it’s worth taking the time to look at how we want our URL to look. Here, we’ll work on the basis that by default, the home page will show the first five blog posts, and if someone wants to see later posts, they need to append a number to the end. Here’s the URL for the second page assuming it’s at example.com:

http://www.example.com/2/

So, we need two separate rules for the URLs. We need one for a URL with no number at the end, and one for a URL with a number at the end, and an optional forward slash. Open up urls.py and edit it so the Home page section looks like this:

# Home page
url(r'^$', 'blogengine.views.getPosts'),
url(r'^(?P<selected_page>\d+)/?$', 'blogengine.views.getPosts'),

Note that I’ve edited the first rule to include ^$ as the regular expression. ^ denotes the start of a regex, and $ denotes the end, so this represents a URL with nothing added after the domain name, such as http://www.example.com. We’ve also changed getRecentPosts to getPosts, as that’s now a more descriptive name.

The second line will match if there is a digit (denoted by the \d+ section) and will pass that digit through to the getPosts function as selected_page. With that done, we now need to make the necessary changes in the view, so move into the blogengine directory and amend views.py to look like this:

# Create your views here.
from django.shortcuts import render_to_response
from django.core.paginator import Paginator
from blogengine.models import Post
def getPosts(request, selected_page=1):
# Get all blog posts
posts = Post.objects.all().order_by('-pub_date')
# Add pagination
pages = Paginator(posts, 5)
returned_page = pages.page(selected_page)
# Display all the posts
return render_to_response('posts.html', {'posts':returned_page.object_list})

Again, note the change in function name from getRecentPosts to getPosts. Now, let’s work through the rest of the code. You’ll notice the following line near the top:

from django.core.paginator import Paginator

This imports the Paginator class, which is very useful for creating pagination. Then, you’ll notice the following line:

def getPosts(request, selected_page=1):

If you know much about Python, you’ll know that you can specify a default value for a parameter passed to a function or method. Here, what we’re doing is setting the default value of selected_page to 1, so if someone visits http://www.example.com, for which the URLconf doesn’t specify a number, this defaults to 1. If they visit http://www.example.com/2 instead, the default value for selected_page will be overriden to 2.

Then you’ll note that we’ve refactored the lines that fetched the posts and sorted them into one line, and called that posts. After that we define pages as a Paginator object, and passed it the values of posts and 5. The first parameter is what we want to divide between pages, and the second is how many instances of this we should allow on an individual page. Here we’re passing through all of the posts, and allowing 5 posts per page. We then define returned_page as the page from pages that matches the number submitted in the selected_page variable. Finally we pass a list of all the objects that make up returned_page through to the template as posts.

So, we now have basic pagination in place. Next, we’ll add the capability to display individual posts.

Now, we could just be lazy and have each post referred to by the numerical ID that’s automatically added by Django to the database, but why would we want to do that? We want a nice, human and search engine friendly URL that gives some idea what the blog post is about. Django is structured in such a way that nice, friendly URLs without cruft are very easy to create, and it actually has a special type of field in the models called a slug field that’s ideal for creating URLs.

So first of all, go into blogengine/models.py and edit it to look like this:

from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField()
text = models.TextField()
slug = models.SlugField(max_length=40, unique=True)
def __unicode__(self):
return self.title

The only change is the addition of the slug field. Like any other field, you’ll be able to edit the slug field using the admin interface. But, why should you have to? Existing blogging solutions like WordPress will suggest a URL for a blog post, so that’s what we want to do as well. Open blogengine/admin.py and edit it to look like this:

import models
from django.contrib import admin
class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
admin.site.register(models.Post, PostAdmin)

If you know a little about object-oriented programming in Python, you should be able to grasp what’s going on here. We’re creating PostAdmin, which inherits from ModelAdmin, and using the title to prepopulate the slug field. We then register this as before, but using PostAdmin rather than the default ModelAdmin.

A fairly typical slug will be based on your title, but will strip out whitespace and other characters between the words and replace them with hyphens, and convert the result to lowercase, so a title like “My new bike” will become my-new-bike.

Also, note that in models.py, we pass the parameter unique=True for the slug. This indicates that the slug must be unique, so we can’t have the same URL applied to two different posts.

With our model and admin amended, it’s now time to create a view to deal with displaying an individual post. Add the following function to blogengine/views.py:

def getPost(request, postSlug):
# Get specified post
post = Post.objects.filter(slug=postSlug)
# Display specified post
return render_to_response('posts.html', { 'posts':post})

This function receives the request object and a slug for the post. It then gets the specific post with that slug, and returns it. For now we’ll just use the existing posts.html template, but we’ll want to add a new template for single posts at some point.

With that done, the next step is to add a URLconf to handle blog posts. Open urls.py and add the following code after the lines for the home page:

# Blog posts
url(r'^(?P<postSlug>[-a-zA-Z0-9]+)/?$', 'blogengine.views.getPost'),

So, now we have a dedicated URL for each post. But how do we get there? We need to create a link from the home page to each individual blog post. Open up your posts.html template and edit it to look like this:

<html>
<head>
<title>My Django Blog</title>
</head>
<body>
{% for post in posts %}
<h1><a href="/{{ post.slug }}">{{ post.title }}</a></h1>
<h3>{{ post.pub_date }}</h3>
{{ post.text }}
{% endfor %}
</body>
</html>

Now, if you run python manage.py syncdb, the changes to your database schema will be made automatically. However, if you already have some test posts in the database, these won’t have a slug and that could cause problems. So you can either add slugs to the existing posts manually using an UPDATE SQL command, or if you’re using something like PHPMyAdmin you can use that to add slugs for these posts. Or if they’re just test posts and you don’t care about them in the slightest, just delete your database and start again from scratch.

With that done, if you then run python manage.py runserver, and then visit http://127.0.0.1:8000, you should see your home page. If you have at least one blog post set up, you should see those posts on the home page, and the title should be a hyperlink to that post. If you have more than 5 posts, you should be able to go to http://127.0.0.1:8000/2 and see the next 5 posts.

But wait! What if you don’t have more posts? You want some code in place to handle what happens if you try to go to http://127.0.0.1:8000/2 and it isn’t there. You also want to dynamically generate links for older and newer posts so that users can click back as far as they need to.

First of all, let’s put something in place to catch nonexistent pages. Open blogengine/views.py and edit the getPosts function to look like this:

# Create your views here.
from django.shortcuts import render_to_response
from django.core.paginator import Paginator, EmptyPage
from blogengine.models import Post
def getPosts(request, selected_page=1):
# Get all blog posts
posts = Post.objects.all().order_by('-pub_date')
# Add pagination
pages = Paginator(posts, 5)
# Get the specified page
try:
returned_page = pages.page(selected_page)
except EmptyPage:
returned_page = pages.page(pages.num_pages)
# Display all the posts
return render_to_response('posts.html', { 'posts':returned_page.object_list})
def getPost(request, postSlug):
# Get specified post
post = Post.objects.filter(slug=postSlug)
# Display specified post
return render_to_response('posts.html', { 'posts':post})

The only differences here are that EmptyPage is imported, and we add error checking to returned_page so that if it throws an EmptyPage exception (meaning that the given page doesn’t exist), then it defaults to returning the highest numbered page. The value of pages.num_pages is the number of pages in total, so you use this to get the last numbered page. If you prefer, you can change it to default to the first page by replacing pages.num_pages with 1.

With this done, the next step is to create links for the next and previous pages. Fortunately Django makes this really easy. First, you have to pass through the returned_page object in views.py, like this:

# Display all the posts
return render_to_response('posts.html', { 'posts':returned_page.object_list, 'page':returned_page})

Here in addition to the existing posts object, we now pass through returned_page as page. Now, amend your posts.html template as follows:

<html>
<head>
<title>My Django Blog</title>
</head>
<body>
{% for post in posts %}
<h1><a href="/{{ post.slug }}">{{ post.title }}</a></h1>
<h3>{{ post.pub_date }}</h3>
{{ post.text }}
{% endfor %}
<br />
{% if page.has_previous %}
<a href="/{{ page.previous_page_number }}/">Previous Page</a>
{% endif %}
{% if page.has_next %}
<a href="/{{ page.next_page_number }}/">Next Page</a>
{% endif %}
</body>
</html>

Here, if the given page has a previous page, we display a link to it, and if it has a next page, we display a link to that too. page.has_previous and page.has_next return True or False, and page.previous_page_number and page.next_page_number display a number for the appropriate page, so it’s easy to use them to link to the appropriate page.

And that will do for now! We’ve gotten quite a lot done this time, and we actually have something that, although it’s still missing many of the more sophisticated features of blogging platforms such as WordPress, is fundamentally usable as a blog as long as you either don’t want comment functionality or are prepared to use a third-party system such as Disqus. Feel free to congratulate yourself with a beverage of your choice, and we’ll carry on later.

28th February 2012 6:25 pm

New Theme

For a long time now, I’ve kept meaning to write my own WordPress theme from scratch for this site, but just haven’t been able to find the time to do so. Now, I’ve finally found the time to do so, and here it is!

Please let me know what you think, but be gentle (this is my first solo WordPress theme, and also the only one I’ve built completely from scratch), and if you find any issues with it please let me know. Don’t expect it to look great in IE6 or IE7 however - I’ve given it a very cursory review in those browsers, and that’s all. It’s still perfectly readable, but it uses a fair amount of CSS3 so it’s inevitably not going to look as pretty in those browsers.

The code is on GitHub if you want to take a look.

24th February 2012 4:17 pm

Yet Another Tutorial for Building a Blog Using Python and Django - Part 1

While I’m extremely fond of Perl for quick hacks and scripts, and have used PHP a fair amount for web development purposes (both on its own and with the CodeIgniter framework), there is one language I love above all others, and that’s Python. I’ve found that, when compared to PHP or Perl, at least for me, it’s a lot easier to “get into the zone” when programming in Python, the code I produce tends to be a lot more readable and easier to follow, and the interactive interpreter makes it really easy to figure out what’s going on in a way that just isn’t possible with PHP or Perl. Also, Python was always designed to be an object-oriented language, and IMHO has a better object model than either Perl or PHP.

While it would be fair to say that Python doesn’t have a single web development framework that monopolises developer’s attention the way Rails does for Ruby programmers, Django is undoubtedly the best-known Python framework. It’s solid, easy to use, and has the best documentation of any web development framework I’ve ever seen (don’t get me wrong, CodeIgniter in particular has very good documentation, but Django’s is exceptional).

In this tutorial, we’re going to build a very simple blogging engine using Django. In its initial stages, it will be an extremely simple web app - we won’t bother with comments, tags, categories or multiple users , or any of the other niceties of a fully-fledged blogging engine. Instead, we will build a very basic Tumblr-style blogging engine, capable of publishing blog posts and very little else. As time goes by, we can add further functionality to this and build it up into a more capable blogging solution.

So, let’s get started. Go to the Django project website and download the latest release (NOTE: as at time of writing this was 1.3.1, but we’re now up to 1.4.3 as at 14 January 2013, and some changes have been made to Django’s structure). Follow the installation instructions given there, and you should be ready to go. Note that from here on, I’m assuming you’re using a Unix-like operating system such as a Linux distro or Mac OS X - if you’re using Windows, there’s a few extra steps you’ll have to take, such as installing Python, and some of the commands you use may be different.

Once Django is installed, find a suitable folder in which to store your new Django project (perhaps a Projects folder in your home directory might be a good place). Note that Django includes its own development server, so you don’t need to install a full LAMP stack like you would if you were developing in PHP. Then, from the folder you want to store your project in, run the following command:

django-admin.py startproject DjangoBlog

This will create a brand-new directory containing all the files you need for your new Django project. If you now cd into this directory, you should see manage.py, as well as a folder called DjangoBlog containing the files __init__.py, settings.py and urls.py.

Let’s go through what these files do. First of all, there’s __init__.py - don’t worry about this, it’s a blank file and you don’t need to touch it.

Next, manage.py contains a number of extremely useful commands that you will find handy when using Django. You’re unlikely to need to edit it, but you’ll use it a lot.

Next, settings.py is the settings for the web app you’re building. It will specify details like what Django applications you’re using, what timezone you’re in, your database connection details and so on. You’ll need to edit this, so open it up in your favourite text editor.

Look for a line that reads “DATABASES”. Under here you’ll notice the following line:

      'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2',     'postgresql', 'mysql', 'sqlite3' or 'oracle'.

You can use pretty much any relational database you like with Django, and because it uses its own Object-Relational Mapping (ORM), it generates the SQL needed for you, taking into account any quirks in that particular database engine. It therefore doesn’t really matter what database you use, and it’s easy to swap them out. For development purposes, we’ll use SQLite as it ships with Python and requires less configuration, so change this line to read as follows:

      'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2',     'postgresql', 'mysql', 'sqlite3' or 'oracle'.

Next you’ll see this line:

      'NAME': '',                      # Or path to database file if using sqlite3.

It really doesn’t matter what you call the file. I tend to call mine backend.db, as follows:

        'NAME': 'backend.db',                      # Or path to database file if using sqlite3.

If you keep going down, you’ll notice TIME_ZONE and LANGUAGE_CODE. You may wish to change these from their default settings (I change mine to Europe/London for TIME_ZONE and en-gb for LANGUAGE_CODE).

Even further down, you’ll notice the INSTALLED_APPS section. Django distinguishes between a project and an application - while a project will normally be a single website, an application will be a set of functionality within that website. For instance, our blog will be a single application, but we could reuse that application on multiple projects. Django also includes a number of applications out of the box - for instance, the flatpages and admin applications can be used together if you wanted to use Django to build a simple CMS, without having to build a new application at all.

For now, we don’t need to add any new applications, so let’s save the changes we’ve made to settings.py and move on to urls.py. This handles directing any incoming HTTP requests to the appropriate place to deal with them. It uses simple regular expressions to evaluate the incoming requests, and maps them to specific view functions. Note that it already includes URLs for the admin functionality, but these are commented out by default.

Exit urls.py and head back to the main directory for your project. Now, we need to test that everything works OK. Run the following command:

python manage.py runserver

Remember I said that the manage.py script had a lot of useful functions? This is one of them. Django has its own simple web server so you don’t have to faff around setting up Apache just for development purposes, and this launches it. If you go to http://127.0.0.1:8000, you should see a screen telling you that Django is running.

Now, you can stop the server for now using Ctrl-C, and we’ll start work on your new app. Run the following command to create your new app:

python manage.py startapp blogengine

Again, note that you used manage.py to do this. There should now be a directory called blogengine in your project. Move into it, and you should find that it contains four files - __init__.py, models.py, tests.py and views.py. Again, __init__.py can be safely ignored, and tests.py can also be left alone, but models.py and views.py deserve closer examination.

If you haven’t used an MVC framework before, then you’ll need this explaining. MVC stands for Model-View-Controller, and it describes a method of logically separating out code for a web application to make it easier to work with. Models represent the data held by the application, views represent what end-users see of the application, and controllers represent the logic that ties the two together.

Django uses a slightly unusual interpretation of MVC. The models work exactly the same as they do in other frameworks, but the logic is handled by the view, and the presentation is handled by templates. Compared to more conventional MVC frameworks such as CodeIgniter, Django’s views are more like controllers, and its templates are more like views. Django is therefore often described as an MTV framework (Model-Template-View), instead of an MVC one.

So, to create our blog, we first need to create a model to describe the data. Edit models.py so it looks like the following:

from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField()
text = models.TextField()
def __unicode__(self):
return self.title

To activate our new app, we also need to include it under INSTALLED_APPS in settings.py:

    'blogengine',

In Django, you create your models as Python classes, which makes it very easy to grasp. Here, a blog post is an object, and it has a title, a publication date, and some text. Note that Post here inherits from models.Model, and has specific types of field that map to field types in the database table. For instance, models.CharField obviously maps to a VARCHAR field in the database, and TextField maps to a TEXT field. You can actually see the SQL that will generate the database table for this model by returning to the project’s main directory and running python manage.py sqlall blogengine:

BEGIN;
CREATE TABLE "blogengine_post" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(200) NOT NULL,
"pub_date" datetime NOT NULL,
"text" text NOT NULL
)
;
COMMIT;

Note the “id” field. You didn’t add this - by default, Django will create an id field in any new table, and will make this the primary key in that database table. You can, however, override this behaviour if you wish. Here it’s exactly what we want so we’ll stick with it for now.

Also note the unicode method. This represents a string that describes that object. Here the title of a blog post seems the most logical way of describing it, so we return the object’s title.

Now that we’ve got our model set up, how do we get the information into it? For a blog post, all of the information will be submitted by the user, so we need to set up some kind of administrative interface. Fortunately, one of Django’s killer features is the admin interface that ships with it. This makes it really quick and easy to get certain kinds of sites up and running.

First of all, we need to activate the admin application. Head up to settings.py and uncomment the line that reads:

    'django.contrib.admin',

Save it, then head for urls.py and uncomment the following lines:

# from django.contrib import admin
# admin.autodiscover()

And:

   # url(r'^admin/', include(admin.site.urls)),

Now, in order for the admin interface to be able to set up new blog posts, you need to also register it. In the blogengine directory containing your app, create a new file called admin.py, and fill it out with the following code:

import models
from django.contrib import admin
admin.site.register(models.Post)

Once that’s done, return to the project directory and run this command to create the database tables you need:

python manage.py syncdb

You’ll get asked for some information to set up your user account - remember it as you’ll need it to log into the admin interface. Once that’s done, run python manage.py runserver again, and return to http://127.0.0.1:8000 again. You should be confronted with a 404 page - that’s fine, that’s exactly what we should be seeing. You’ll note that the message states that Django tried the ^admin/ path without success - what this means is that this is the only URL pattern in urls.py at the moment, and the path you entered didn’t match this.

If you change the URL in the browser to http://127.0.0.1:8000/admin, you should get a login screen. Enter the username and password you set when you ran syncdb and click Log in. You should now see Django’s admin interface, with Posts available, and an Add and Change dialogue visible next to it. If you want to add a few blog posts, just to have some data to work with, go ahead. Note that for the Date and Time dialogues, Django automatically adds the Today and Now shortcuts.

So, our model is now sorted and we have some data in the web app. The next step is to write our views. You’ll notice that the blogengine app contains a file called views.py - open this up and enter the following code:

# Create your views here.
from django.shortcuts import render_to_response
from blogengine.models import Post
def getRecentPosts(request):
# Get all blog posts
posts = Post.objects.all()
# Sort posts into chronological order
sorted_posts = posts.order_by('-pub_date')
# Display all the posts
return render_to_response('posts.html', { 'posts':sorted_posts})

Let’s go through this code. The first line imports the render_to_response method, which is used to render a template. The second line imports the Post model.

Next, we define the getRecentPosts view. For simplicity’s sake, we aren’t going to bother about pagination for the moment, so we’ll just get all the posts. The view is written as a Python function, and we pass it the request object as the sole parameter.

Next, we get all of the Post objects, using Post.objects.all(), and assign it to a list called posts. As we want these to be in reverse chronological order, we then reorder them by pub_date (note the - sign at the beginning to denote reverse order) and assign the result to sorted_posts. Finally, we load the posts.html template and pass through sorted_posts as the value in a dictionary called posts.

With our view done, we now need to produce a template for it. Head back up to your main project directory and create a new folder called templates. Then, go into settings.py and find the line marked TEMPLATE_DIRS. Inside the brackets, underneath the comments, add the full, absolute path to the new templates folder, as in this example:

  "/Users/matthewdaly/Development/Python/Django/blog/templates"

You’ll have to change this to the full, absolute path on your machine. This will tell Django to look for the templates in that folder. Now, go into templates, and create a new file called posts.html. Enter the following text into it:

<html>
<head>
<title>My Django Blog</title>
</head>
<body>
{% for post in posts %}
<h1>{{ post.title }}</h1>
<h3>{{ post.pub_date }}</h3>
{{ post.text }}
{% endfor %}
</body>
</html>

Most of this is just plain old HTML, but you’ll notice that {% %} denotes tags that can include some logic (such as a for loop in this case), and {{ }} denotes a variable. Remember that in the view we passed through a dictionary containing all of the Post objects, and here we’re iterating through all of those post objects, outputting their title, publication date and text content.

With this done, we need to configure the routes to call the getRecentPosts view when someone visits the home page. Open urls.py again and add the following code underneath where you enabled the admin, but still inside the parentheses:

# Home page
url(r'', 'blogengine.views.getRecentPosts'),

Now, this is a very simple regular expression. Here, this is our default page, so we leave the single quotes after the r empty. We then specify that this URL should be handled by the getRecentPosts function, inside views.py, in the blogengine application.

Save that, and start up the development server again with python manage.py runserver. Then, if you haven’t already added a few test posts, do so via the admin interface. Then open http://127.0.0.1:8000, and you should see your blog posts.

So, we now have the beginnings of a blogging application! We’ll leave it here for now, and will go on to add functionality like viewing individual posts and pagination later. We’ll also look into adding further functionality to our blog, such as supporting multiple authors, tagging posts, and adding flat pages.

Recent Posts

What I Want in a PHP CMS

Flow Typed AJAX Responses With React Hooks

Caching the Laravel User Provider With a Decorator

The Trouble With Integrated Static Analysis

Don't Use Stdclass

About me

I'm a web and mobile app developer based in Norfolk. My skillset includes Python, PHP and Javascript, and I have extensive experience working with CodeIgniter, Laravel, Zend Framework, Django, Phonegap and React.js.