调用'createsuperuser'时引发UnicodeEncodeError

时间:2019-02-04 15:49:59

标签: python django postgresql django-models character-encoding

我正在尝试使用PostGreSQL数据库在服务器上部署Django应用程序(2.1.5和Python 3.6.6)。 我像往常一样执行了“ makemigrations”和“ migrate”,然后无法使用命令“ createsuperuser”创建超级用户:

[alex@web574 myproject]$ python3.6 manage.py createsuperuser
Nom d'utilisateur (leave blank to use 'alex'): 
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/alex/webapps/global_hse_project/lib/python3.6/Django-2.1.5-py3.6.egg/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/alex/webapps/global_hse_project/lib/python3.6/Django-2.1.5-py3.6.egg/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/alex/webapps/global_hse_project/lib/python3.6/Django-2.1.5-py3.6.egg/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/alex/webapps/global_hse_project/lib/python3.6/Django-2.1.5-py3.6.egg/django/contrib/auth/management/commands/createsuperuser.py", line 60, in execute
    return super().execute(*args, **options)
  File "/home/alex/webapps/global_hse_project/lib/python3.6/Django-2.1.5-py3.6.egg/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/home/alex/webapps/global_hse_project/lib/python3.6/Django-2.1.5-py3.6.egg/django/contrib/auth/management/commands/createsuperuser.py", line 139, in handle
    input_value = self.get_input_data(field, message)
  File "/home/alex/webapps/global_hse_project/lib/python3.6/Django-2.1.5-py3.6.egg/django/contrib/auth/management/commands/createsuperuser.py", line 194, in get_input_data
    raw_value = input(message)
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 8: ordinal not in range(128)

我在Google上发现要添加:

# -*- Coding: utf-8 -*-

位于文件顶部,但不起作用,结果与变量DEFAULT_CHARSET(https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_CHARSET)相同。 我的PostGreSQL数据库要求utf-8编码。

2 个答案:

答案 0 :(得分:2)

这可能是由于标准输入法所使用的编码不支持在input()提示符下键入的字符引起的。

在运行UTF-8命令之前,您可以尝试使用PYTHONIOENCODING环境变量将编码明确设置为createsuperuser

export PYTHONIOENCODING="UTF-8"; python3.6 manage.py createsuperuser

答案 1 :(得分:0)

我认为这是与这里相同的问题: previous post 您必须添加

# -*- coding: utf-8 -*-
在每个文件的开头

包含正确的编码