Python Django 编码错误,非 ASCII 字符 'xe5' [英] Python Django Encoding Error, Non-ASCII character 'xe5'
本文介绍了Python Django 编码错误,非 ASCII 字符 'xe5'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 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)
我收到以下错误:
/hello3_template/处的语法错误
第 19 行文件 D:WinPython-32bit-2.7.5.3django_testarticleviews.py 中的非 ASCII 字符 'xe5',但未声明编码;见 http://www.python.org/peps/pep-0263.html 详情(views.py,第 19 行)SyntaxError at /hello3_template/
Non-ASCII character 'xe5' in file D:WinPython-32bit-2.7.5.3django_testarticleviews.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 19)我查找了那个链接,但我仍然不知道如何解决它.
I look up that link, but I am still puzzled on how to resolve it.
你能帮忙吗?谢谢,小蜜蜂
Could you help? Thanks, smallbee
正如 lalo 所指出的,以下行必须在顶部
# -*- coding: utf-8 -*-
谢谢大家.
推荐答案
好吧,你来了:
将 # -*- coding: utf-8 -*-
放在文件的顶部,它定义了编码.
Put # -*- coding: utf-8 -*-
at top of file, it define de encoding.
文档 说:
如果没有其他编码,Python 将默认使用 ASCII 作为标准编码给出编码提示.
Python will default to ASCII as standard encoding if no other encoding hints are given.
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:
因此,您的代码必须开始:
So, you code must begin:
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
...
希望有帮助
这篇关于Python Django 编码错误,非 ASCII 字符 'xe5'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文