If you just want to return html that will get dumped into a container
via innerHTML, then I don't really see the need to return json.  Just
return the html as text/html and dump the response directly into the
container.

However, the approach you are taking is nice because it lets you add
more metadata to the response.

I do something sort of like this:

response_dict.update({'html': html_table, 'success': 1, 'el_id':
456})
return HttpResponse(simplejson.dumps(response_dict),
                            mimetype='application/javascript')

Then on the js side of things:

if (json.success) {
    document.getElementById(json.el_id).innerHTML = json.html;
} else {
    // handle errors here
}



On Aug 10, 7:43 pm, r_f_d <[EMAIL PROTECTED]> wrote:
> I was wondering what people are doing to format html snippets returned
> via XHR.  I am currently using code  like:
>
> response_dict = {}
>
> t = ModelObject.objects.all()
>
> html_table = render_to_response('table_template.html', {'table': t})
>
> response_dict.update({'table': html_table})
>
> return HttpResponse(simplejson.dumps(response_dict),
>                             mimetype='application/javascript')
>
> Where my template contains code to loop through the queryset and
> layout the table appropriately.  I also do this with
> form_for_model(instance) to layout the form fields.
>
> This seems pretty appropriate to me, but as I am relatively new to
> python and django, I was wondering if others are doing something
> similar or if there are other suggestions for returning the html
> formatted.
> Thanks


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to