Public pages: Topic template

  1. If… Then
  2. Public pages
  3. Include subtemplates

Often, blogs will have a page showing all posts on a particular topic. Create a new template, “topic.html”:

{% extends "index.html" %}

{% block title %}ODGB Topic: {{ topic.title }}{% endblock %}

{% block headline %}Dead Guys Topic: {{ topic.title }}{% endblock %}

Create a new function in views.py:

def topicalPosts(request, topicSlug):

topic = get_object_or_404(Topic, slug=topicSlug)

posts = Post.objects.filter(live=True, topics=topic).order_by('-date')

context = {'postings': posts, 'topic': topic}

return render_to_response('topic.html', context)

And add Topic to the list of models being imported:

from Blog.postings.models import Post, Topic

In urls.py, make this view use the URL /topic/slug:

(r'^topic/(.+)$', 'topicalPosts'),

This will send any request for the URL …/postings/topic/slug to the “topicalPosts” function. Go ahead and view it as http://127.0.0.1:8000/postings/topic/slug, replacing “slug” with the slug for one of your topics, such as http://127.0.0.1:8000/postings/topic/war.

  1. If… Then
  2. Public pages
  3. Include subtemplates