Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Friday, October 17, 2008

Template inheritance in Django

I've just discovered a killer feature of Django... template inheritance, using the {% block %} and {% extends %} tags.

It's probably there in other templating systems and frameworks so I'm probably coming late to the party but for someone who is more than a bit obsessive about nice clean structures in website development, avoidance of repetition in code, etc. it really appeals to me, more than all the other nifty things I've seen that Django can do.

To quote from the Django documentation:

One common way of using inheritance is the following three-level approach:
  • Create a base.html template that holds the main look-and-feel of your site.
  • Create a base_SECTIONNAME.html template for each "section" of your site. For example, base_news.html, base_sports.html. These templates all extend base.html and include section-specific styles/design.
  • Create individual templates for each type of page, such as a news article or blog entry. These templates extend the appropriate section template.

Tuesday, October 14, 2008

List comprehensions in Python

Just came across this concept in the Django documentation, but without any background in Python or maths it wasn't immediately obvious what it was.

The basic syntax is:
L = [expression for variable in sequence]
L is the name of a the resulting variable containing the list of items generated by the loop
expression is the python expression that will be evaluated on each iteration of the for loop, using the given variable
variable refers to each item in the list to pass to the expression as we loop through it
sequence is the set of items we're looping through

An example:
toppings = ('ham', 'pineapple', 'beef', 'chicken')
x = ', '.join([toppings.loop_count() for t in toppings])
Totally pointless operation I know, as it simply copies the list in the 'toppings' variable to a new variable called 'x', but it illustrates the point! In reality it would make more sense if the initial list was a queryset object from a database that we want to loop through and list the values.

The for...in loop syntax already made sense to me, but I'd not come across this shorter way of writing it before. Apparently it's also the most efficient in terms of system resources.

Lists, tuples, dictionaries etc. in Python

I'm currently learning Django and found a useful reference explaining the different types of 'collection' structures in Python.

In brief:

List - contained in square brackets - like a PHP array - contents are editable.

Tuple - contained in round brackets - like a PHP array but contents are fixed (can't be edited). If only one item is specified, it must be followed by a comma.

Dictionary - contained in curly brackets - like an associative array in PHP

String - contained in quotes - can refer to individual characters by their position in the string like in a list/tuple

Iterator - a list temporarily created from an object to allow looping through each value using 'for ... in ...' syntax

With all of these, when referring to a specific item within the collection, you use square brackets.