The basics of creating a tumblelog with Django (part 2)

In The basics of creating a tumblelog with Django, I took my first shot at walking others through some helpful code, in this case the basic programming patterns to use when creating a tumblelog with Django. Since posting this entry, I’ve received some requests to explain how to get these tumblelog items to actually show up on my homepage. Since my target audience is someone relatively new to Django, or even new to programming, I should have included this in the original entry.

This walk-thru picks up where the original left off, with generic StreamItem objects pointing to various Bookmarks, Statuses, and FreeComments. Each StreamItem object, in addition to keeping track of when the original item was published, also has a get_rendered_html method which passes the original object into a template and provides the resulting output. The StreamItem class and the functions that create StreamItems are kept in the models.py file of an app called ‘stream.’

Now lets get these items onto the homepage.

The first step is to determine what URL, when entered, will show these items. Since we’re putting these on our homepage, the url is just “/”. So in the urls.py file, we want to match a URL that begins and ends with “/” and execute the ‘homepage’ function in views.py.

1 2 3 4 5 from django.conf.urls.defaults import * urlpatterns = patterns ( '' , ( r'^$' , 'stream.views.homepage' ), )

The line in the urlpatterns is a regular expression. ‘^’ roughly means, “at the beginning,” and “$” roughly means “at the end.” Since there’s nothing between the beginning and the end, this URL will match http://example.com/ and run the homepage() method in stream/views.py. Let’s take a look at this method.

1 2 3 4 5 6 7 8 9 10 from stream.models import StreamItem from django.shortcuts import render_to_response def homepage ( request ): context = { 'stream_item_list' : StreamItem . objects . all () . order_by ( '-pub_date' )[: 10 ] } return render_to_response ( 'stream/homepage.html' , context )

We create a dictionary named ‘context’, which contains a variable named ‘stream_item_list,’ which gets a list of the latest 10 StreamItems sorted by their ‘pub_date’ field, newest to oldest. This dictionary is then passed to the render_to_response function, along with a template name that will be rendered with access to any variable set in the context. homepage.html contains this:

1 2 3 { % for stream_item in stream_item_list % } {{ stream_item . get_rendered_html }} { % endfor % }

It loops through each item in the list, and outputs each item’s get_rendered_html method, which passes that item’s referenced object, a Bookmark for example, to a template. This method can be seen in the original tutorial. Finishing our Bookmark example, here’s the template that each Bookmark is rendered with.

1 2 3 4 5 6 7 8 9 10 11 { % load comments % } { % get_free_comment_count for blog . bookmark object . id as comment_count % } < div class = "stream_link" > < div class = "stream_item" > < a href = "{{ object.get_absolute_url }}" title = "View more about this link" class = "icon icon_link" > View more about this link </ a > < h4 >< a href = "{{ object.href }}" title = "{{ object.title }}" > {{ object . title }} </ a ></ h4 > < h5 > {{ object . time | date : "F jS Y, P" }} // < a href = "{{ object.get_absolute_url }}#discussion" title = "View comments for {{ object.title }} " > {{ comment_count }} comment {{ comment_count | pluralize }} </ a ></ h5 > { % load markup % } {{ object . extended | markdown }} </ div >< ! -- End . stream_item --> </ div >< ! -- End . stream_link -->

I hope this helps clear up any questions, and if it raises any more, please leave them in the comments.