Monday, 15 September 2008

8. A Social Code-Sharing Site

This is part 8 of a series of posts on James Bennett's excellent Practical Django Projects. The table of contents and explanation can be found here

Building and testing the models for the snippets app is fairly straightforward. The only changes you need to make are to separate out your admin classes into the separate admin.py file which I've shown previously, so type up all the code up to p154 and register your admin classes.
#cab.admin.py
from django.contrib import admin
from cab.models import Snippet, Language

admin.site.register(Language)
admin.site.register(Snippet)

Testing the Snippets Application

Here we syncdb and add some Language and Snippet objects. When adding a Language object the language_code corresponds to the short names on the lexers page, and I guess you can pick a mime_type that fits the language. In my case I just used python as the language_code and mime_type text/x-python.

Initial Views for Snippets and Languages

Because there is no sample code it is necessary for us to actually create the templates snippet_list.html and snippet_detail.html. Because the templates are trivial my suggestion is to just create the most basic template to begin with, put all the available model variables in it, and then come back to it later if you want to pretty it up. I'll write up one very basic one for snippet_list.html as a basic example.

Before we create the template I want to mention the {% debug %} tag. This is an extremely handy tag to insert when you want to work out what environment, variables, data etc is being made available to your template. There is a catch however. The debug tag displays actual objects with html tags around them, so you don't actually see some of the information you might want to see. For example in my snippets_list.html I was looking at the 'object_list' key generated by debug and only seeing an empty list instead of 'object_list':[<Snippet: Example Snippet>].

So heres the simplest template for your snippet_list.html which you add in above or below the example block of code from p156 that uses the page variables for pagination.
  {% for snippet in object_list %}
  <p><a href="{{ snippet.get_absolute_url }}">{{ snippet.title }}</a></p>
 {% endfor %}
The next bunch of templates I've just filled with placeholders {% debug %} and/or the actual variables like <p>{{ object.title }}</p>.

And that's really it for this chapter. Everything works fine with no further modification needed, so it's onto the next major feature of Django in the next chapter. Forms.

3 comments:

josebrwn said...

Thanks so much for this series, it has been so helpful. A word about the {% debug %} variable: it looks way better if you wrap it in pre tags:

< pre >
{% debug %}
< /pre >

Darren said...

Hi!

I know the posts are old but they have helped me a lot!

With the top authors I can only display the 'count'. How can I display the author name in the template?

Darren said...

-_- never mind. After trying {{ author.name }}, {{ author.user }}, I used {{ author }} and it worked.

Hedged Down