嗨, 我遇到了Python Django的编码错误。 在我的views.py,我有以下几点:Python Django编码错误,非ASCII字符' xe5'
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)
我得到了以下错误:在/ hello3_template
的SyntaxError/
非ASCII字符' \ xe5 '文件D:\ WinPython-32bit-2.7.5.3 \ django_test \ article \ views.py在第19行,但没有声明编码;有关详细信息,请参阅 http://www.python.org/peps/pep-0263.html(views.py,第19行)我查找该链接,但我仍然对如何解决该链接感到困惑。
你能帮忙吗? 谢谢, smallbee
作为拉洛指出,以下行必须是在顶部
# -*- coding: utf-8 -*-
谢谢所有。
不该'# - * - 编码:UTF-8 - * - '在文件顶部? – lalo
嗨,你好,你是对的。它在我把这条线放在最上面之后起作用。谢谢。 – smallbee
@lalo:将其写为答案;如果你链接到文档并解释它,那几乎肯定是他的问题。 – abarnert