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
-~----------~----~----~----~------~----~------~--~---