How to Display a Two Dimensional Dictionary in Django Templates

Let's say you have a class like this:

from testcms.contents.models import *
class Contents(UserDict):

    def get_contents_list(self):
        contents = Contents.objects.all()
        for content in contents:

            author = 
            Users.objects.get(userid = content.author_id)
            type = 
            ContentTypes.objects.get(type_id = content.type_id)
            relative = 
            ContentsRelative.objects.filter(content_id =
 content.content_id).count()
            self[content.id] = {
                     'content_id' : content.id,
                     'title' : content.name,
                     'type' : type.name,
                     'relative' : relative,
            }
    return self

And you want to display that Python dictionary at the presentation level with Django templates.  Here is what you would do:

{% for content_id, content in contents.items %}
    {{ content.title }}
    {{ content.type }}
    {{ content.relative }}
{% endfor %}

And that's that.

0 Comments, 0 trackbacks (Trackback URL)

0 responses to How to Display a Two Dimensional Dictionary in Django Templates

Leave a Comment
  1. (required)
  2. Ignore this field:
  3. Don't put anything in this field:
    Don't put anything here:
  4. Leave this empty:
    (required)
  5. Your email is not publically displayed.