Python Django编码错误,非ASCII字符'\ xe5'

sma*_*bee 7 python django encoding view

嗨,我遇到了Python Django的编码错误.在我的views.py中,我有以下内容:

from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
# Create your views here.

def hello(request):
    name = 'Mike'
    html = '<html><body>Hi %s, this seems to have !!!!worked!</body></html>' % name
    return HttpResponse(html)

def hello2(request):
    name = 'Andrew'
    html = '<html><body>Hi %s, this seems to have !!!!worked!</body></html>' % name
    return HttpResponse(html)

# -*- coding: utf-8 -*-
def hello3_template(request):
    name = u'??'
    t = get_template('hello3.html')
    html = t.render(Context({'name' : name}))
    return HttpResponse(html)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

/ hello3_template /中的SyntaxError

第19行的文件D:\ WinPython-32bit-2.7.5.3\django_test\article\views.py中的非ASCII字符'\ xe5',但未声明编码; 有关详细信息,请访问http://www.python.org/peps/pep-0263.html(views.py,第19行)

我查了那个链接,但我仍然对如何解决它感到困惑.

你能帮忙吗?谢谢,小熊

正如lalo指出的那样,以下行必须在顶部

# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)

谢谢你们.

lal*_*alo 12

那么,你在这里:

放在# -*- coding: utf-8 -*-文件顶部,它定义de编码.

文件说:

如果没有给出其他编码提示,Python将默认为ASCII作为标准编码.

To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:
Run Code Online (Sandbox Code Playgroud)

所以,你的代码必须开始:

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
...
Run Code Online (Sandbox Code Playgroud)

希望有所帮助