diadia

興味があることをやってみる。自分のメモを残しておきます。

UnicodeEncodeError:が表示されるとき

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)がdjangoで表示された。

環境、状況

環境

  • centos7
  • django2.1
  • python3.6

状況

python36 manage.py createsuperuser

と叩き、ユーザ名を入力すると上のエラーが出た。このエラーが出る前には

python36 manage.py makemigrations
python36 manage.py migrate

のコマンドを通すことができていた。

解決方法

参考はこちらから

$ export LANG='en_US.UTF-8'

これでcreatesuperuserのコマンドを通すことができた。エラーの原因はcentos7の環境として'ascii'になっていたが、djangoではasciiを対応していないから?centosの文字の環境をasciiから変更したらユーザー名を入力することができた。

    File "/home/****/.local/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 112, in handle
    username = self.get_input_data(self.username_field, input_msg, default_username)
   File "/home/****/.local/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 193, in get_input_data
    raw_value = input(message)

エラー詳細の理由がわからなかったが、解決してから見るとraw_value = input(message)でエラーになってた事が原因か。input関数

あとは

export LANG="ja_JP.UTF-8"

で整えること。