Public pages: Views

  1. Add more data
  2. Public pages
  3. Using the Django API

In a web framework such as Django, you don’t have separate pages. What you have are views into a (usually) database. You’ll often have one template that supplies one view with as many pages as there are records in the database.

We’re going to start with a single view that displays all live posts. In your Blog folder make a folder called “templates”, and copy the template.html file into that folder as “index.html”.

Open views.py in postings and add:

from django.shortcuts import render_to_response

def listPosts(request):

return render_to_response('index.html')

Open urls.py and add this to the bottom of urlpatterns:

(r'^$', 'Blog.postings.views.listPosts'),

It should look something like this:

urlpatterns = patterns('',

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

(r'^$', 'Blog.postings.views.listPosts'),

)

The urlpatterns items use regular expressions to send different URL requests to different functions.

Look in settings.py for TEMPLATE_DIRS. Add the full path to your templates folder to it:

TEMPLATE_DIRS = (

'/Users/jerry/Desktop/Django/Blog/templates',

)

If you now go to http://localhost:8000/ you should see the Old Dead Guys Blog main page.

  1. Add more data
  2. Public pages
  3. Using the Django API