python - Removing quotes from keys of dicts when rendering to template -
is possible remove quotes on keys during rendering dict render_to_string
function key:value
, not 'key':value
in template?
for example if dict:
d = {'a':1, 'b':2}
and render this,
return render_to_string('somefile.json', {'d':d})
then in somefile.json
{{d}}
{'a':1, 'b':2}
, want {{d}}
{a:1, b:2}
. (without quotes on a
, b
)
how achieve this?
tia
one approach use overriding __repr__
method of dict
class or subclassing , changing method there. have latter solution below.
class mydict(dict): def __repr__(self): s = "{" key in self: s += "{0}:{1}, ".format(key, self[key]) if len(s) > 1: s = s[0: -2] s += "}" return s mydict({'a': 1, 'b': 2}) {a:1, b:2}
Comments
Post a Comment