Hi Alex, here is a small example of a JSON response.  I don't mix HTML
and JSON personally (I use HTML pages and then fetch JSON via AJAX
calls so).

Here is a fragment of code from one of my views:

    t = loader.get_template('members/member_info.json')
    c = Context({'member_set':member_set})
    return HttpResponse(t.render(c), mimetype="text/plain")

Here is the template:

{"results":[
{% for one_member in member_set %}
{
"id":"{{one_member.id}}",
"username":"{{one_member.username}}",
"first_name":"{{one_member.first_name}}",
"last_name":"{{one_member.last_name}}"
},
{% endfor %}
]}

I choose text/plain deliberately but you might choose text/json (or
something else).  if you embed HTML in JSON then you will need to be
careful that the HTML does not itself contain characters that break
the JSON (e.g. quotation marks).

Cheers
Ian

On Jun 16, 8:46 pm, Alex <alexle...@googlemail.com> wrote:
> Thanks all. I may go with Matt's idea of serialising html + other data
> to json and decoding client side. I'm not totally keen though because
> this abandons a very nice rollup of functionality in django's
> render_to_response (I am not familiar with how to write the template
> as JSON and rendering to that. Keeping the template as html seems like
> the right thing to do). I was hoping you'd scream 'don't be daft -
> everyone does this...'  :)
>
>                                   Alex

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to