[Fixed]-Meet " UnicodeDecodeError at /admin/login/ " when following Django Documentation Tutorial

1👍

Breakdown:

The exception is issued by python itself. It happens when attempting to decode some raw data stream into strings. If you are new to python, you should know that python3 makes a clear distinction between strings aka str (that contain characters) and raw data aka bytes (that just contain bytes, potentially binary data).

The exception raised here means that for some reason, python was ordered to decode some bytes into text using utf-8 encoding, yet the data is not valid utf-8-encoded text.

Assuming you come from a western country, my bet is the text is using ANSI or ISO-8859-1 and has an “î” in it. That gets encoded as 0xee in ANSI, but should be encoded as 0xC3 0xAE in UTF-8.

There are several reasons this could happen. Here, from the traceback, it happened while rendering a template. More specifically, while rendering a template from an app’s directory. So you have in one of your apps a template that’s not properly encoded.

How it happened? Well, I see you are running a Windows box. The Windows environment is somewhat of a mess when it comes to text encoding. Every software comes with its own opinion of what to use as default (when it can be changed). For instance, Notepad still encodes in ANSI by default, or ISO-8859-1 in Western Europe.

It is very likely that one of the software you use for editing your templates is encoding your files into whatever. You have two options from here:

  • Check the options of your tools and make sure they are all configured to use UTF-8 encoding.
  • Or configure Django to use the same encoding as your tools. You would do that by adding a FILE_CHARSET='iso-8859-1' line to your settings, or whatever encoding your tools use.

In any case, you must be sure that all of your tools agree on the encoding used, or you will either have other decoding errors, or some characters will get mangled (and show as strange î or ? symbols).

Not useful for Django tutorial, but worth reading at some point in your python life: Unicode handling in Python

Leave a comment