python - Putting utf-8 dict in Django template -


i've got little faq.conf file containing dict structure i'm importing ast django. question , answer contain utf-8 chars , html markup.

{ '1': {'question':'','answer':''}, '2': {}, '3': {}, } 

so use ast import it:

with open(os.path.join(fs_root, faq_file)) q: faq = ast.literal_eval(q.read()) 

after putting view context , using filter {{ faq|safe }} receive:

{'1': {'answer': 'zewn\xc4\x99trznych mog\xc4\x85 si\xc4\x99 do\xc4\x87 osoby. w szczeg\xc3\xb3lno\xc5\x9bci takie, kt\xc3\xb3rych. ...} 

what should proper output in templates?

assuming using python 2, problem reading file byte string , not unicode string. believe ast handles utf-8 fine, can do:

with open(os.path.join(fs_root, faq_file)) q: faq = ast.literal_eval(q.read().decode('utf-8')) 

or use codecs.open open file correct encoding.


if ast doesn't unicode characters, can call decode() on strings before passing them django.


if using python 3, problem encoding isn't utf-8, in case using codecs above should work, provided use correct encoding.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -