I decided to write a template tag for my purposes. It's kind of
specific to a dictionary within a dictionary, but here is what I
created:

from django import template

register = template.Library()

def get_term_value(parser, token):
    try:
        # split_contents() knows not to split quoted strings.
        tag_name, vocabularies, vocab_name, term_name =
token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires exactly
two arguments" % tag_name
    return GetTermNode(vocabularies, vocab_name, term_name)
register.tag('term_value', get_term_value)

class GetTermNode(template.Node):
    def __init__(self, vocabularies, vocab_name, term_name):
        self.vocabularies = template.Variable(vocabularies)
        self.vocab_name = template.Variable(vocab_name)
        self.term_name = template.Variable(term_name)

    def render(self, context):
        try:
            resolved_vocab = self.vocabularies.resolve(context)
            resolved_vocab_name = self.vocab_name.resolve(context)
            resolved_term_name = self.term_name.resolve(context)
            return resolved_vocab[resolved_vocab_name]
[resolved_term_name]
        except template.VariableDoesNotExist:
            return None

So now I can use a template tag to return a value in the template like
so:
{% term_value vocabularies_dictionary "agent-qualifiers" "act" %}
(similar to vocabularies_dictionary['agent-qualifiers']['act'] )
or
{% term_value vocabularies_dictionary vocab_name term_name %} (similar
to vocabularies_dictionary[vocab_name][term_name] )

Thanks for your help.

On Feb 17, 9:14 am, Daniel Roseman <[email protected]>
wrote:
> On Feb 17, 2:01 pm, "[email protected]"
>
>
>
> <[email protected]> wrote:
> > Why can't you change the dictionary? Can't you just create a copy of
> > the dict and rename the keys in your copy using _ rather than -  like
> > so:
>
> > >>> bad = {'var-1':'Apples', 'var-2':'Pears'}
> > >>> good = dict()
> > >>> for k,v in bad.items():
> > >>>    good[k.replace('-','_')] = v
> > >>> good
>
> > {'var_1': 'Apples', 'var_2': 'Pears'}
>
> > This does assume that you simply want a representation of the dict
> > data in the template, but I can't see any issues of sending a new dict
> > to the template. As you say it is certainly an annoying feature of the
> > Django templating language.
>
> > On Feb 16, 8:15 pm, bfrederi <[email protected]> wrote:
>
> > > I have a dictionary of dictionaries that I want to use in a template.
> > > Unfortunately some of the keys are hyphenated (I can't change it), so
> > > when I try to call them in the template using {{ dictionary.key-
> > > name }} it doesn't work:
>
> > > Could not parse the remainder: '-qualifiers' from
> > > 'display.vocabularies.agent-qualifiers'
>
> > > Is there a simple work around method for this in Django that I haven't
> > > picked up yet? Or is this something that I will have to create a
> > > special work around for? Any suggestions?
>
> Or, write a quick custom template filter.
>
> @register.filter
> def dict_get(d, key):
>     return d.get(key)
>
> {{ dictionary|dict_get:"key-name" }}
> --
> DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to