Lỗi khi chạy manage.py createsuperuser

Hôm nay quay lại làm một ứng dụng nhỏ trên Django với môi trường MacOS (Mountain Lion). Mọi thứ có vẻ rất ổn cho đến khi gặp lỗi với dòng lệnh để tạo một superuser

python manager.py createsuperuser 

Lỗi được hiển thị ra như sau:

Traceback (most recent call last):

  File "manage.py", line 10, in <module>

    execute_from_command_line(sys.argv)

  File "/HuyVu/study/workenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line

    utility.execute()

  File "/HuyVu/study/workenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute

    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "/HuyVu/study/workenv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv

    self.execute(*args, **options.__dict__)

  File "/HuyVu/study/workenv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute

    output = self.handle(*args, **options)

  File "/HuyVu/study/workenv/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 70, in handle

    default_username = get_default_username()

  File "/HuyVu/study/workenv/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 105, in get_default_username

    default_username = get_system_username()

  File "/HuyVu/study/workenv/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 85, in get_system_username

    return getpass.getuser().decode(locale.getdefaultlocale()[1])

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py", line 496, in getdefaultlocale

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py", line 428, in _parse_localename

ValueError: unknown locale: UTF-8

Hoá ra django không lấy được Locale mặc định của hệ thống. Để khắc phục lỗi này, chúng ta khai báo thêm biến LANG và LC_ALL vào môi trường bằng cách cho thêm 2 dòng sau vào ~/.bash_profile

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

Sau đó nhớ kích hoạt sự thay đổi này

source ~/.bash_profile

Và bây giờ có thể thực hiện tạo superuser một cách bình thường.

Blog's tag:

Blog's topic:

Phản hồi