Django Deployment – create superuser fails with locale error

I was playing with my django configuration on my ubuntu server when I ran into this problem creating a superuser:

Traceback (most recent call last):
  File "manage.py", line 18, in 
    execute_manager(settings)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 361, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 222, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in handle
    default_username = get_default_username()
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/__init__.py", line 105, in get_default_username
    default_username = get_system_username()
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/__init__.py", line 85, in get_system_username
    return getpass.getuser().decode(locale.getdefaultlocale()[1])

The issue comes from no default locale being set on the server.

There are a couple bug reports in for this already, with some minor argument among the developers on fixing it or just making users set up their locale properly.

I tried setting my locale correctly, but I basically stumble around linux using google as a seeing eye dog, so after a couple minutes of searching and trying locale commands I still couldn’t get it to work. This was the only time I have had this problem, so I decided to just fix it manually with a little hack. Use the locale setting that fits your implementation.

All I did was edit manage.py and stick this at the top

import os
os.environ.setdefault('LANG','en_US')

This makes the python locale.getdefaultlocale() call return en_US, which stops django from being a crybaby and lets you get on with your day.

Disclaimer – I have no idea if this causes any negative side effects. I can’t see it doing so and I’m using this myself, but if you magically light your box on fire and bring forth the apocalypse because of this, I’m not taking any blame. Use at your own risk.

Leave a Comment