Search code examples
pythondjango-templatesapache2django-viewslocale

python locale.setlocale fails when running apache


I've got a simple Ubuntu/django/apache server set up and I'm having trouble formatting some of the numbers that I want to display in my Django templates. When I run the code locally (i.e. on my work machine) using the Django test server everything formats with no problem.

Likewise, when I open up IDLE on the server I can do this:`

>>> import locale

>>> locale.setlocale(locale.LC_ALL,'')

'en_US.UTF-8'

>>> '{0:n}'.format(42424242)

'42,424,242'`

However whenever I try to run the apache server and test the code live it fails and I get outputs like:

'42424242'

I prepended a print statement to the

locale.setlocale(locale.LC_ALL,'')

call that have in my view.py file and all I found in the apache error log was

[Tue Jul 24 15:26:56 2012] [error] C

Could it be that the apache process doesn't have permissions to access the native locale setting?


Solution

  • I managed to solve the problem by explicitly calling

    locale.setlocale(locale.LC_ALL,'en_US.UTF-8')

    I'm not sure why it wasn't working without the en_US.UTF-8 parameter as the local setting is 'en_US.UTF-8'. If anyone knows why I needed to use an explicit call when the apache process runs the code but not when I'm testing it anywhere else I'd still be interested in an answer, but I'll mark this as solved.