1👍
It sounds like you are getting an instance of a model from an ajax request to the server, and you want to use that instance in the “_comments.html” template. Is that right? If so, you could instead render the template on the server and fetch the rendered HTML in the ajax request.
So, suppose you have a view called fetch_new_comments
that handles the ajax request. Instead of getting a comment model and dumping it to JSON and returning that, the view might look something like this:
def fetch_new_comments(request):
comments = ... # get whatever data you're using
return render_to_response("_comments.html", {"comments": comments})
So this means that your ajax request would receive a chunk of HTML (instead of a JSON object), and insert that on the page. If you’re using jQuery, you could do something like this:
$.get("http://yoursite.com/fetch_new_comments/", function (resp) {
$("#new_comments_container").html(resp);
});
Source:stackexchange.com