I am trying to load some element details on click of this element in some already defined div. I have 2 ways to do this.
Note. I use python / django as a backend, and jquery in frontend
I am. My first way
Views.py
def get_item_data(id): obj = MyClass.objects.get(pk=id) html = """ <div>All Details Here, 1. {0}, 2. {1}</div> """.format(str(obj.id), str(obj.name)) return HttpResponse(html)
myapp.js
$('#myid').on('click', function(){ $.ajax({ type: "GET", url: GET_DATA, data: dataString, success: function(res) { $('#addl_container').html(res); } }); });
II. Another way
Views.py
def get_item_data(id): obj = MyClass.objects.filter(pk=id).values() return HttpResponse(simplejson.dumps(obj))
myapp.js
$('#myid').on('click', function(){ $.ajax({ type: "GET", url: GET_DATA, data: dataString, success: function(res) { $('#addl_container').html( "<div>All Details Here, 1. "+ res.id +", 2. "+ res.name + "</div>" ); } }); });
Both processes work. I thought that creating html in advance and loading it might not be a good choice, since we use python code to create html and then load it. Also it will not work if I try to access the same URL from another application. Creating html from jquery by extracting data from a json response seems like a good solution, but sometimes when the data is huge, the user needs more bar, which slows down the system.
I am confused which one to use, please suggest the best solution of these two. Also suggest another solution better than this, if any?
Thanks in advance.