Django HTML模板中的特殊字符

时间:2013-12-28 19:10:13

标签: html django django-templates

我正在尝试在Django HTML模板中使用特殊字符编写某些内容,例如“é”或“ê”,但它不起作用。 Django返回此错误消息:

UnicodeDecodeError at /
'utf8' codec can't decode byte 0x97 in position 248: invalid start byte

这是整个追溯:     环境:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.6.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'survey',
 'south',
 'django_reset',
 'chartkick',
 'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/filipeferminiano/Documents/django/panorama/survey/views.py" in home
  35.         return render(request, 'home.html')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
  53.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  162.         t = get_template(template_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in get_template
  138.     template, origin = find_template(template_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in find_template
  127.             source, display_name = loader(name, dirs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in __call__
  43.         return self.load_template(template_name, template_dirs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in load_template
  46.         source, display_name = self.load_template_source(template_name, template_dirs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loaders/filesystem.py" in load_template_source
  38.                     return (fp.read().decode(settings.FILE_CHARSET), filepath)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py" in decode
  16.     return codecs.utf_8_decode(input, errors, True)

Exception Type: UnicodeDecodeError at /
Exception Value: 'utf8' codec can't decode byte 0x87 in position 230: invalid start byte

呈现模板的视图:

def home(request):
    if request.user.is_authenticated():
        browser_stats = [['Chrome', 52.9], ['Firefox', 27.7], ['Opera', 1.6],
                     ['Internet Explorer', 12.6], ['Safari', 4]]
        return HttpResponseRedirect('/profile/')
    else:
        return render(request, 'home.html')

模板的网址:

url(r'^$', 'survey.views.home', name='home'),

1 个答案:

答案 0 :(得分:3)

三件事:

  • 请输入

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

在.py文件的开头。

  • 该.py文件必须是没有BOM文件的UTF-8(编码 - 从Notepad ++转换为没有BOM的UTF-8)

  • 此外,包含unicode字符的文本字符串应以u开头。所以你应该写一些像text=u'unicode here'

如果您仍然看到相同的erorr,请提供完整的堆栈跟踪。此外,<meta charset="utf-8">与客户端相关,与django例外无关。