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)
我收到以下错误:
我查了那个链接,但我仍然对如何解决它感到困惑.
你能帮忙吗?谢谢,小熊
正如lalo指出的那样,以下行必须在顶部
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
谢谢你们.
lal*_*alo 12
那么,你在这里:
放在# -*- coding: utf-8 -*-文件顶部,它定义de编码.
该文件说:
如果没有给出其他编码提示,Python将默认为ASCII作为标准编码.
Run Code Online (Sandbox Code Playgroud)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:
所以,你的代码必须开始:
# -*- 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)
希望有所帮助
| 归档时间: |
|
| 查看次数: |
14346 次 |
| 最近记录: |