Rek*_*koc 1 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)
Run Code Online (Sandbox Code Playgroud)
我在谷歌上找到添加:
# -*- Coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
在文件的顶部,但它不起作用,结果与变量 DEFAULT_CHARSET ( https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_CHARSET ) 相同。我的 PostGreSQL 数据库要求使用 utf-8 编码。
这可能是由于用于 stdin 的编码不支持在input()提示符下键入的字符这一事实造成的。
在运行命令之前,您可以尝试将编码显式设置为UTF-8使用PYTHONIOENCODING环境变量createsuperuser:
export PYTHONIOENCODING="UTF-8"; python3.6 manage.py createsuperuser
Run Code Online (Sandbox Code Playgroud)