This might be a silly question but I'm rather new to JavaScript.. I have an JSON encoded response of a python dictionairy which I want to use to inject as data in my javascript
However the django return consists of string with a header AND hashmap
HOW, but more importantly WHERE should the logic of stripping this down to usable data take place? (All I want is the map)
- In views.py (my entire way of returning a JSON object should be revised)
- In template (my approach is okay but I should write a custom filter)
- In my html (I should strip the result of it's header within the javascript on doc load)
Thanks
edit:
the jQuery part
var chart;
$(document).ready(function() {
chart = new Mycharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'bar'
},
settings: {
data: [],
},
...
the view开发者_开发知识库
data = assemble_dict()
testVar2 = json_response(data)
return render_to_response('home.html', { "testVar": testVar, "testVar2": testVar2 })
the applogic
def assemble_dict():
data = {}
objects = Balance.objects.all()
for obj in objects:
data[obj.name.name_bank] = obj.money
return data
def json_response(*args):
return HttpResponse(
simplejson.dumps(args),
)
this is what I get back now from {{ testVar2 }}: Content-Type: text/html; charset=utf-8 [{"Stuff": 50, "MoreStuff": 3}]
Why is your view separate from the (I presume you mean) app logic? Seems like your json_response
function is a view, in that it is returning an HttpResponse. You either need to directly return the value from json_response
, or leave out the HttpResponse
call in that function.
精彩评论