1

I'm trying to write somethings with special characters in Django HTML templates like "é" or "ê", but it's is not working. And Django returns this error message:

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

This is the entire traceback: Environment:

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

The view that renders the template:

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')

The url of the template:

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

1 Answer 1

4

Three things:

  • Please put

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

at the start of your .py file.

  • That .py file has to be a UTF-8 without BOM file (Encoding - Convert to UTF-8 without BOM from Notepad++)

  • Also, your text strings that contain unicode characters should start with a u. So you should write something like text=u'unicode here'.

If you still see the same erorr please provide the full stacktrace. Also, <meta charset="utf-8"> is client related and doesn't have anything to do with django exceptions.

Sign up to request clarification or add additional context in comments.

5 Comments

But in the views.py there is just a redirect for the html file. And the text is not a variable rendered from django to html, it's a pure HTML text.
Please update your question with the full stacktrace of the exception. Also, is your html template in UTF-8 without bom ? Convert it with notepad++ if not. Finally, provide the parts of views.py and urls.py that render the mentioned template.
Ok, i already updated. And the files are encoded with utf-8 without bom
This seems like a problem in your template. For reasons unknown to me, your template contains non-unicode characters so django cannot decode it. Have you double checked that your template is actually converted to UTF-8 w/o BOM ? Also, as you can understand there are some offending characters: Try to start with a clean template for home.html that works and put more text gradually until you find out which are the offending characters... I can't think of any better solution :(
Ok, I already solved the problem. I deleted all the old files and replaced with new ones with same name and code. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.