Django has an escapejs filter so if you're using a template to
generate json you'd be safer doing, e.g.:

"first_name":"{{ one_member.first_name|escapejs }}"

That way if someone sticks something untoward (like a double quote) in
your first_name or last_name fields it won't break things :).

For the rolling up of HTML and JSON I personally would use a template
for the HTML but I wouldn't bother with it for the JSON as typically
the stuff you want to chuck back in JSON would be most naturally (I
think) built up as a python data structure (e.g. dict of values) which
you use simplejson (which is included with Django) to convert into
json.

from django.utils import simplejson
returnData={
  'success':1,
  'htmlData:template.render(context),
  'someListData':['Item 1','Item 2']
}
return HttpResponse(simplejson.dumps(data),mimetype='text/plain)

That way simplejson'll take care of sorting everything out :). If you
want to dump out django objects as json too then look at the
documentation for serialising django objects.

Regards,
Matt

On Jun 17, 8:42 am, Ian McDowall <i.d.mcdow...@gmail.com> wrote:
> 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).

-- 
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