Public pages: Using the Django API

  1. Views
  2. Public pages
  3. Templates

The usefulness of Django is not that it can send flat files to web browsers. It is that Django provides an API for creating complex dynamic content that can be inserted into templates.

First, modify index.html. Replace “<p>World, say hello.</p>” with:

{% for post in postings %}

<h2>{{ post.title }}</h2>

{% endfor %}

Add this to the top of views.py:

from Blog.postings.models import Post

Change listPosts to:

def listPosts(request):

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

context = {'postings': posts}

return render_to_response('index.html', context)

If you see an empty blog page, make sure that you’ve marked some posts as “live”. Otherwise, you should see a series of headlines.

  1. Views
  2. Public pages
  3. Templates