I am returning json from a view to a django template so I can work with the data in javascript, but now I also need one of the arrays to be accessed with django template syntax. This seems to not work so well.
In my view I create a开发者_开发知识库 python array with object items. I return simplejson.dumps like this:
response_dict = {"Description":pkg.desc,"MyTypes":pkgtypes,...
return HttpResponse(simplejson.dumps(response_dict), mimetype='text/javascript')
where pkgtypes is the array of objects I am interested in accessing through template tags. I managed to find "|safe" filter which at least shows me that 4 items have been returned to the template, but I cannot access the objects inside. Here is what I've tried:
{% for t in MyTypes|safe %}
SOMETHING
<input type="checkbox" name="myTypes" value="{{ t.Val }}" />{{ t.Label }}
{% endfor %}
the word "SOMETHING" gets written 4 times with checkboxes, but I cannot access "Val" or "Label" which are values that come from the array, like this:
[{"Val":myval1, "Label":mylabel1},{"Val":myval2,"Label":mylabel2}...]
Should I just build my checkboxes using javascript instead?
Why are you applying safe
to a sequence of items? safe
works for strings, so if you want to mark stuff as safe, do it before outputting it, like {{ t.Label|safe }}
You can also try putting {% debug %}
somewhere to see what exactly the MyTypes
variable contains.
精彩评论