Alex Rivera | Logout

In Django, where is the best place to put short snippets of HTML-formatted data?

Asked 2008-09-28T21:31:21.887
9

This question is related to (but perhaps not quite the same as):

Does Django have HTML helpers?

My problem is this: In Django, I am constantly reproducing the basic formatting for low-level database objects. Here's an example:

I have two classes, Person and Address. There are multiple Addresses for each Person, setup likeso (in their respective models.py)

class Person(models.Model):
  ...

class Address(models.Model):
  contact = models.ForeignKey(Person)

Now, whenever I look at a Person, I want to see all their Addresses. So suppose Persons/views.py has something likeso:

def detail(request, person_id):
   person = get_object_or_404( Person, pk=person_id )
   return render_to_response('persons/details.html',
      { 'title' : unicode(person), 'addresses': person.address_set.all() } )

And, I have a template, persons/details.html, with code, for example, like-so:

{% extends "base.html" %}

{% for address in addresses %}
<b>{{ address.name }}</b>
  {{ address.type }} <br>
  {{ address.street_1 }}<br>
  {{ address.street_2 }}<br>
  {{ address.city }} {{ address.stateprov }} {{ address.postalcode }}<br>
  {{ address.country }}
  <hr>
{{ endfor }}

I am repeating this code quite a bit, often with minor variations, such when it's in a table, and then < br > must be substituted by < /td >< td >. Other times, I don't want a street_2 to display (or the < br > after it). All to say, there is fundamental logic that I want to express, that I am even more loath to tote around with block-and-copy!

What I want is a persons/details.html with, for example, the following:

{% extends "base.html" %}
{% for address in addresses %} 
 
Edit
Report

1 Answer

13

Sounds like an inclusion tag is what you're looking for. You could have a template and tag for each major variation and use the tag's arguments to customise the context for each template as required.

Basic tag definition:

@register.inclusion_tag('person/address.html')
def display_address(address):
    return {'address': address}

Use in templates (assuming the templatetag module containing it has already been {% load %}-ed):

{% display_address address %}
answered 2008-09-28T21:53:27.140

Your Answer