The view is where you do all the logic to be sent to your templates. In this example we will not be dealing with RequestContext. This would give you access to the request object which contains details for the currently logged in user, as well as a few other features you will be likely to use in the future.

For this example we need to create 3 views.

  • Display your categories & latest posts
  • Display the posts in a specific category
  • Display the post

Here is a copy of the view.py.

# Create your views here.

from djangorocks.blog.models import Blog, Category
from django.shortcuts import render_to_response, get_object_or_404

def index(request):
    return render_to_response('index.html', {
        'categories': Category.objects.all(),
        'posts': Blog.objects.all()[:5]
    })

def view_post(request, slug):   
    return render_to_response('view_post.html', {
        'post': get_object_or_404(Blog, slug=slug)
    })

def view_category(request, slug):
    category = get_object_or_404(Category, slug=slug)
    return render_to_response('view_category.html', {
        'category': category,
        'posts': Blog.objects.filter(category=category)[:5]
    })

As with the admin.py file we need to import the models.

from djangorocks.blog.models import Blog, Category

We also need to import a couple of functions for displaying our template.

from django.shortcuts import render_to_response, get_object_or_404

The following is for your index page, which will display a list of all your categories, and 5 most recent posts.

When defining functions, you always need to specify the variable request. This is the request object, which contains details of the User, POST & GET data as well as a few other bits. In the following instance we are also specifying slug which is mapped to from the urls.py file as you will see in a minute

def view_category(request, slug):

This first part sets the template file that we are going to be using.

render_to_response('test.html', {

The next part queries the database for both categories and posts. Category and Blog are the names of the models we created earlier.

'categories': Category.objects.all(),
'posts': Blog.objects.all()[:5]

In the other two functions view_post & view_category we use one of the rather useful Django shortcuts. This queries the database trying to match where slug=slug, the first slug being the field in the model, the second slug being the input into the function call, more on this in a second when we define the URL's.

get_object_or_404(Blog, slug=slug)

Defining your URLs

Open up the urls.py located in your project folder, add the following 3 lines.

(r'^$', 'djangorocks.blog.views.index'),
url(
    r'^blog/view/(?P<slug>[^\.]+).html', 
    'djangorocks.blog.views.view_post', 
    name='view_blog_post'),
url(
    r'^blog/category/(?P<slug>[^\.]+).html', 
    'djangorocks.blog.views.view_category', 
    name='view_blog_category'),

The first URL match is just a simple match nothing, ie http://www.yourdomain.com/, and mapping that through to the view index. The second two have custom variables being passed to the view. There are just regular expressions with the parameter matching syntax of django.

(?P<slug>[^\.]+)

As I always use the .html extension on my URL's this works fine for me. This matches everything up to '.', there are other ways of doing this, and it maps the result to slug which is also the name of a parameter in the view_post and view_category functions.

The final part labeled name is what we used when defining the models. get_absolute_url returns a URL automatically calculated based on the URL that is entered here. Defining this just once means that if you change the mapping URL, it will also change throughout the site. You are also able to use the template tag that Django provides to do a similar thing, however this is not used in our example.