KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 %}
Tags (comma-separated)
Save Edits
Cancel