I need to solve a Django problem - manage.py createsuperuser | UnicodeEncodeError

0

When executing:

(env) luis@ux-pc:~/projects/administrador-webapp-django$ python3 manage.py createsuperuser
Nombre de usuario (leave blank to use 'luis'): superusername

I get a Unicode error:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/luis/projects/administrador-webapp-django/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 38
1, in execute_from_command_line
    utility.execute()
  File "/home/luis/projects/administrador-webapp-django/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 37
5, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/luis/projects/administrador-webapp-django/env/lib/python3.5/site-packages/django/core/management/base.py", line 316, i
n run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/luis/projects/administrador-webapp-django/env/lib/python3.5/site-packages/django/contrib/auth/management/commands/crea
tesuperuser.py", line 61, in execute
    return super().execute(*args, **options)
  File "/home/luis/projects/administrador-webapp-django/env/lib/python3.5/site-packages/django/core/management/base.py", line 353, i
n execute
    output = self.handle(*args, **options)
  File "/home/luis/projects/administrador-webapp-django/env/lib/python3.5/site-packages/django/contrib/auth/management/commands/crea
tesuperuser.py", line 140, in handle
    input_value = self.get_input_data(field, message)
  File "/home/luis/projects/administrador-webapp-django/env/lib/python3.5/site-packages/django/contrib/auth/management/commands/crea
tesuperuser.py", line 195, in get_input_data
    raw_value = input(message)
UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in position 7: ordinal not in range(128)

This happens when I try to create a user, I searched for the UnicodeEncodeError: 'ascii' codec can not encode character '\ xf3' in position 7: ordinal not in range (128) and I have not encountered the problem

If someone has gone through and has solved it, I ask that please share the solution, I need to advance in the project I have.

    
asked by Luemapo 15.11.2018 в 04:01
source

2 answers

0

Try placing this line in the ".py" files of your django project such as "manage.py" and "settings.py".

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

Verify previously that you have the migrations

python manage.py makemigrations
python manage.py migrate
    
answered by 17.11.2018 в 17:17
0

In the stacktrace that samples can be seen in the last lines (which are where you have to start deciphering the error):

    raw_value = input(message)
UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in position 7: ordinal not in range(128)

The last line indicates the problem (when trying to decode a byte of value f3 as ascii, it has not been able to, which is normal because ascii does not have such high codes, the last one being the 7f ).

The previous line tells you at what point in your source code the problem arose and we can see what it was by doing a input() . It reads from the standard input what the user type, and apparently between the typed there was a value byte f3 that is not valid ascii.

The fact that a byte of value f3 appears makes me suspect that your encoding is cp1252 (the windows one), or ISO-8859-1 or ISO-8899-15 (also known as latin1 and latin9, respectively , typical in old versions of Linux since the modern ones use utf8). These encodings that I just mentioned have all of them in the position f3 the character ó (or with tilde).

Usually python auto-detects the encoding used by the standard input, but you see that in your case it has failed (according to my hypothesis) the standard input follows one of the encodings that I mentioned, and python instead believes that it is ascii .

Sometimes this happens if the standard input is redirected from another command, through a pipe, such as:

$ cat fichero-datos.txt | python programa.py

In this case, python usually assumes that its standard input is ascii, and fails if what comes from the previous command contains non-ascii bytes.

For these cases where autodetection fails, you can prepare an environment variable called PYTHONIOENCODING with the appropriate value. In our case it would be (assuming that latin1 is the encoding actually used by the standard input):

$ export PYTHONIOENCODING=latin1
$ python programa.py
# o tambien
$ cat fichero.txt | python  programa.py
    
answered by 17.11.2018 в 21:08