Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 45817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:52:07+00:00 2026-05-10T15:52:07+00:00

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

  • 0

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 %}    {% address.as_html4 %} {% endfor %} 

And if I want inline table, something likeso (I guess!):

{% extends 'base.html' %} <table><tr> {% for address in addresses %}   <tr><td> {% address.as_html4 </td><td> %} </td></tr> {% endfor %} </table> 

The question is, then: Where is the best place to put the formatting? The logic?

Django seem to have the following (plausible) options:

  1. Put the formatting in models.py

  2. Put the logic/formatting in views.py

  3. Put the logic/formatting in some other sub-class of Person or Address (i.e. addresses/html4.py)

  4. Create custom tags

Help / insight much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:52:08+00:00Added an answer on May 10, 2026 at 3:52 pm

    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 %} 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[This question is related to but not the same as this one .] My
[This question is related to but not the same as this one .] If
This is kind of related to my previous question, but not really. I have
[This question is somewhat related to this question , but the answers are not...]
This is related to the this question and the answer maybe the same but
This question might not seem programming related at first, but let me explain. I'm
This is not exactly a programming question, but it's highly related. We are writing
I have a problem which I think is related to forward declarations, but perhaps
I know this question isn't directly programming related, but since I want to be
This is related to my earlier question , but I thought I'd simplify it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.