0
votes

Am trying to run an app in development but I keep getting UnicodeDecodeError: 'utf8' codec can't decode byte 0x85 in position 1950: invalid start byte

Please how do trace the exact part of the code where this is coming from as I can't make sense of it's error source Below is the full error screen

{u'selected': {}, u'categories': {u'ratings': ((1, u'*'), (2, u'**'), (3, u'***'), (4, u'****'), (5, u'*****')), u'genre s': <QuerySet []>, u'actors': <QuerySet []>, u'directors': <QuerySet []>}}
Internal Server Error: /movie/
Traceback (most recent call last):
File "c:\python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
File "c:\python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
File "c:\python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Roland\Documents\Web2\myproject_env\myproject2\movies\views.py", line 70, in movie_list
    return render(request, "movies/movie_list.html",context)
File "c:\python27\lib\site-packages\django\shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
File "c:\python27\lib\site-packages\django\template\loader.py", line 67, in render_to_string
    template = get_template(template_name, using=using)
File "c:\python27\lib\site-packages\django\template\loader.py", line 21, in get_template
    return engine.get_template(template_name)
File "c:\python27\lib\site-packages\django\template\backends\django.py", line 39, in get_template
    return Template(self.engine.get_template(template_name), self)
File "c:\python27\lib\site-packages\django\template\engine.py", line 162, in get_template
    template, origin = self.find_template(template_name)
File "c:\python27\lib\site-packages\django\template\engine.py", line 136, in find_template
    name, template_dirs=dirs, skip=skip,
File "c:\python27\lib\site-packages\django\template\loaders\base.py", line 38, in get_template
    contents = self.get_contents(origin)
File "c:\python27\lib\site-packages\django\template\loaders\filesystem.py", line 29, in get_contents
    return fp.read()
File "C:\Users\Roland\Documents\Web2\myproject_env\lib\codecs.py", line 314, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x85 in position 1950: invalid start byte
1

1 Answers

0
votes

Reading a long traceback like this can be tricky, for sure.

First you need to know how to read the lines of the traceback. They come in pairs: a description of where to find the code, and a snippet of the code. Like this, the last pair of lines:

File "C:\Users\Roland\Documents\Web2\myproject_env\lib\codecs.py", line 314, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)

This tells you which file, line number, and function to look in to find the line of code that was involved. This last line is the one that raised the actual error. However, it's not a line of your code - it's from a library. It's pretty rare that you need to read the code of libraries to debug exceptions in your code. So we look at the next line up:

File "c:\python27\lib\site-packages\django\template\loaders\filesystem.py", line 29, in get_contents
    return fp.read()

This is the line that called the line we just looked at. It's also not your code - it's from django. So you keep looking up, line by line, until you find a line from your code. Here it is:

File "C:\Users\Roland\Documents\Web2\myproject_env\myproject2\movies\views.py", line 70, in movie_list
    return render(request, "movies/movie_list.html",context)

This isn't the line that caused the exception, but it's the last part of the code that you're responsible for that ultimately led to the exception. So this is most likely where you'll need to make a change. In fact, looking further up the traceback, this is the only line you have control over - the rest are all in libraries. So this is definitely where you'll need to make the change. Now you can narrow your search to find more information: you need to call render with different arguments, somehow. I'd search for django render UnicodeDecodeError. And in fact, the first hit is another Stack Overflow question with exactly the same problem! Unfortunately the answer isn't very helpful. But if you keep searching, you'll find one that is.