简体   繁体   中英

Django : Can't figure out encoding in Django

I have this application working fine in python 2.7 Totally!

It takes "من" for example and changes it to "mn".

# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
"""Kurdish Alphabet to Kurdish Kirmanci Latin Translator"""

s = "من"
Latin = {
        'ئه':'A','ا':'A','ب':'B',
        'ج':'C','چ':'Ç','د':'D',
        'ە':'E','ێ':'Ê','ف':'F',
        'گ':'G','ه':'H','هه':'Ha',
        'ئ':'I','ی':'Î','ژ':'J',
        'ک':'K','ل':'L','م':'M','ن':'N',
        'ۆ':'O','پ':'P','ق':'Q','ر':'R',
        'س':'S','ش':'Ş','ت':'T',
        'وو':'U','و':'Ú','ی':'Y','ز':'Z',
        'خ':'X',' ':' ','؟':'?','،':',',}

#this will take each index of the list
#and take it through ChangeTool
#and print it

wordlist = list(s)
wordlist = [ch for ch in s]    
for l in wordlist:
        print (Latin[l])
print("\r")

Now I have changed this to a Django Function, I'm getting strange behavior!

Django Function:

def change(request):
        Latin = {'ئه','ا','ب','ج','چ',}
            Latin = [character for character in Latin]
return render_to_response('change_result.html',{'Latin':Latin})

I'm only testing what will be the outcome! and now this is what shows up in the html page :

['\xd8\xa6\xd9\x87', '\xda\x86', '\xd8\xa8', '\xd8\xac', '\xd8\xa7'] 

If I put u'' in front of the letter this is what comes out :

[u'\u0628', u'\u0626\u0647', u'\u062c', u'\u0627', u'\u0686'] 

Can Someone please please kindly tell me whats happening ? Why the letters are not showing up on the html page ?

A friend found the problem!

I was passing LIST in the return of Django View

Which causes showing the object not the actual index :/ something like this comes out :

[u'\u0628', u'\u0626\u0647', u'\u062c', u'\u0627', u'\u0686'] 

So I changed the view to join the indexes to a string then PASS the string not the LIST :

Latin = ''.join(Latin)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM