2

Weighted Directed Graphs are represented as dictionaries of dictionaries in python. Something like this (example):

digraph = {'a': {'b':2, 'c':3}, 'b': { 'a':1, 'd',2}}

My problem involves passing this digraph object to the Django Template system. In this example 'a', 'b', 'c', 'd' are nodes of the graph and the digraph represents connections between these nodes along with the weight of each connecting edge given by the integer values.

Consider a general node: node.

I am having difficulties in accessing: digraph.node.items , inside the template. For any dictionary D, D.items works well. But not when we want to access the items of a sub-dictionary (in the above digraph). This is what I want exactly (but does NOT work well):

{% for node in node_list %}
  {% for adj_node,weight in digraph.node.items %}
    {{ adj_node }}, {{ weight }} <br/>
  {% endfor %}
{% endfor %}

adj_node and weight are not printed.

Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99
  • Why not `{% for adj_node,weight in node.items %`? You can't do a lookup with a variable in the Django template system. This may require constructing the data structure specifically for display in the template. – Simeon Visser May 25 '13 at 20:23
  • From django docs about templating language design: The goal is not to invent a programming language. The goal is to offer just enough programming-esque functionality, such as branching and looping, that is essential for making presentation-related decisions. https://docs.djangoproject.com/en/dev/misc/design-philosophies/#don-t-invent-a-programming-language – MostafaR May 25 '13 at 20:42
  • @SimeonVisser: **node** is not a dictionary. So node.items does not make sense? – Pranjal Mittal May 26 '13 at 16:16
  • @MostafaR: Yes, thats what I understand then. – Pranjal Mittal May 26 '13 at 16:19
  • @pramttl: currently not but you could redesign your data structure before passing it to the template. You can't lookup `digraph.node` when `node` is a variable but you can have multiple nested loops in your template. – Simeon Visser May 26 '13 at 17:35

1 Answers1

1

There is the option of defining your own filter to extract the value of interest from the dictionary: http://ralphminderhoud.com/posts/variable-as-dictionary-key-in-django-templates/

The short version:

In project/app/templatetags/dictionary_extras.py:

from django import template
register = template.Library()

@register.filter(name='access')
def access(value, arg):
    return value[arg]

In urls.py:

url(r'^testing', 'views.vTestingLists', name='testing_lists'),

In views.html:

def vTestingLists(request):
    context= {'d': {'a':'apple', 'b': 'banana', 'm':'mango', 'e':'egg', 'c':'carrot'}}
    return render_to_response( 'testinglists.html', context )

In the template named testinglists.html:

{% load dictionary_extras %}
{% for key in d %}
    {{ d|access:key }}
{% endfor %}

Then access the …/testing URL to see the result of your handiwork.

This is not my code, all credit to Ralph Minderhoud (author of the linked article). I have verified this code works as transcribed (not copy/pasted) from this answer to my Django 1.1 environment.

ManicDee
  • 732
  • 6
  • 16