WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[next]

Django for the Impatient: The Finishing Touches

By Jeff Forcier, Paul Bissex and Wesley Chun

Social Bookmark

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume


[This is the second part of a two-part series. The previous installment covered the basics of building the blog. In this installment, we'll put the finishing touches on it.]

Making Your Blog's Public Side

With the database and admin side of our application taken care of, it's time to turn to the public-facing pages. A page, from Django's perspective,has three typical components:

We'll tackle these three in that order. In a sense this is building from the inside out— when Django processes a request, it starts with the URL patterns, then calls the view, and then returns the data rendered into a template.

Creating a Template

Django's template language is easy enough to read that we can jump right in to example code. This is a simple template for displaying a single blog post:

It's just HTML (though Django templates can be used for any kind of textual output) plus special template tags in curly braces. These are variable tags, which display data passed to the template. Inside a variable tag, you can use Python-style dot-notation to access attributes of the objects you pass to your template. For example, this template assumes you have passed it a BlogPost object called "post."The three lines of the template fetch the BlogPost object's title, timestamp, and body fields, respectively.

Let's enhance the template a bit so it can be used to display multiple blog posts, using Django's for template tag.

The original three lines are unchanged; we've simply added a block tag called for that renders a template section once for each of a sequence of items. The syntax is deliberately similar to Python's loop syntax. Note that unlike variable tags, block tags are enclosed in {% ... %} pairs.

Save this simple five-line template in a file called archive.html, and put that file in a directory called templates inside your blog app directory. That is, the path to your template file should be:

The name of the template itself is arbitrary (we could have called it foo.html), but the templates directory name is mandatory. By default, when searching for templates, Django looks for a templates directory inside each of your installed applications.

Creating a View Function

Now we'll write a simple view function that fetches all our blog posts from the database and displays them using our template. Open up the blog/views.py file and type the following:

Skipping over the import lines for the moment (they just load up the function and classes we need), here's the breakdown of the view function, line by line:

Creating a URL Pattern

Only one more piece is needed for our page to work—like anything else on the Web, it needs a URL.

We could create the needed URL pattern directly inside mysite/urls.py, but that creates a messy coupling between our project and our app.We can use our blog app somewhere else, so it would be nice if it were responsible for its own URLs. We do this in two simple steps.

The first step is much like enabling the admin. In mysite/urls.py, there's a commented example line that is almost what we need. Edit it so it looks like this:

This catches any requests that begin with blog/ and passes them to a new URLconf you're about to create.

The second step is to define URLs inside the blog application package itself. Make a new file, mysite/blog/urls.py, containing these lines:

It looks a lot like our base URLconf. The action happens in line 5. First, note the blog/ part of the request URL, which our root URLconf was matching, is stripped—our blog application is reusable and shouldn't care if it's mounted at blog/ or news/ or what/i/had/for/lunch/. The regular expression in line 5 matches a bare URL, such as /blog/.

The view function, archive, is provided in the second part of the pattern tuple. (Note we're not passing a string that names the function, but an actual first-class function object. Strings can be used as well, as you see later.)

Let's see it in action! Is the dev server still running? If not, fire it up with manage.py runserver, and then go to http://127.0.0.1:8000/blog/ in your browser. You should see a simple, bare-bones rendering of any blog posts you have entered, complete with title, timestamp, and post body.


[next]

Recent Articles

WebReference.com site name
Rolling Out Your Own HTML Application Version Control
HTML 5: Client-side Storage
Working with Ajax Server Extensions
internet.com site name
Wi-Fi Product Watch, November 2009
Chip Market Recovering From '08 Collapse
Low-Cost Tools to Kickstart Your New Business


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs