Django: Non-ASCII character

后端 未结 7 1760
清歌不尽
清歌不尽 2020-12-23 15:12

My Django View/Template is not able to handle special characters. The simple view below fails because of the ñ. I get below error:

Non-ASCII character

相关标签:
7条回答
  • 2020-12-23 15:18

    Insert at the top of views.py

    # -*- coding: utf-8 -*-
    

    And add "u" before your string

    my_str = u"plus de détails"
    

    Solved!

    0 讨论(0)
  • 2020-12-23 15:24

    You need the coding comment Gabi mentioned and also use the unicode "u" sign before your string :

    return HttpResponse(u'español')
    

    The best page I found on the web explaining all the ASCII/Unicode mess is this one : http://www.stereoplex.com/blog/python-unicode-and-unicodedecodeerror

    Enjoy!

    0 讨论(0)
  • 2020-12-23 15:31

    I was struggling with the same issue as @dkgirl, yet despite making all of the changes suggested here I still could not get constant strings that I'd defined in settings.py that contain ñ to show up in pages rendered from my templates.

    Instead I replaced every instance of "utf-8" in my python code from the above solutions to "ISO-8859-1" (Latin-1). It works fine now.

    Odd since everything seems to indicate that ñ is supported by utf-8 (and in fact I'm still using utf-8 in my templates). Perhaps this is an issue only on older Django versions? I'm running 1.2 beta 1.

    Any other ideas what may have caused the problem? Here's my old traceback:
    Traceback (most recent call last):
    File "manage.py", line 4, in
    import settings # Assumed to be in the same directory.
    File "C:\dev\xxxxx\settings.py", line 53
    ('es', ugettext(u'Espa±ol') ),
    SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xf1 in position 0: unexpected end of data

    0 讨论(0)
  • 2020-12-23 15:34

    Set DEFAULT_CHARSET to 'utf-8' in your settings.py file.

    0 讨论(0)
  • 2020-12-23 15:37

    The thing about encoding is that apart from declaring to use UTF-8 (via <meta> and the project's settings.py file) you should of course respect your declaration: make sure your files are saved using UTF-8 encoding.

    The reason is simple: you tell the interpreter to do IO using a specific charset. When you didn't save your files with that charset, the interpreter will get lost.

    Some IDEs and editors will use Latin1 (ISO-8859-1) by default, which explains why Ryan his answer could work. Although it's not a valid solution to the original question being asked, but a quick fix.

    0 讨论(0)
  • 2020-12-23 15:42

    Do you have this at the beginning of your script:

    # -*- coding: utf-8 -*-
    

    ...?

    See this: http://www.python.org/dev/peps/pep-0263/

    EDIT: For the second problem, it's about the html encoding. Put this in the head of your html page (you should send the request as an html page, otherwise I don't think you will be able to output that character correctly):

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    0 讨论(0)
提交回复
热议问题