Unicode string display on Django template

Viewed 13313

I am using django v1.5.*, I am going to render the a variable named "foobar" which is a json obj and including unicode string.

def home( request ):
    import json
    foo = {"name": u"赞我们一下"}
    bar = json.dumps( foo )
    return render_to_response( 'myapp/home.html',
        { "foobar": bar, },
        context_instance=RequestContext(request)
    )

And in my template, I encode the json obj in javascript and then append to the div, it can display the expected string:

  foobar=JSON.encode('{{foobar|safe}}'); 
  $("#foobar").html(foobar.name);`

then I can get the 赞一下我们 on my web page.
But I found that if I use the variable directly:

<div id="foobar">{{ foobar }}</div>

it will display the unicode string as byte string:
{ "name":"\u8d5e\u4e00\u4e0b\u6211\u4eec" }
Even if I using the {{foobar|safe}} then nothing change.

Now, I want to ask why this happend or is anything wrong of me? What should I do if I do want to using the variable directly as {{ foobar }} ?

3 Answers
Related