开发者

Problems trying to format currency with Python (Django)

开发者 https://www.devze.com 2023-01-01 15:18 出处:网络
I have the following code in Django: import locale locale.setlocale( locale.LC_ALL, \'\' ) def format_currency(i):

I have the following code in Django:

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

def format_currency(i):
    return locale.currency(float(i), grouping=True)

It work on some computers in dev mode, but as soon as I try to deploy it on production I get this error:

Exception Type: TemplateSyntaxError
Exception Value: Caught ValueError while rendering: Currency formatting is not possible using the 'C' locale.
Exception Location: /usr/lib/python2.6/locale.py in currency, line 240

The weird thing is that I can do this on the production server and it will work without any errors:

python manage.py shell
>>&开发者_Go百科gt; import locale 
>>> locale.setlocale( locale.LC_ALL, '' )
'en_CA.UTF-8'
>>> locale.currency(1, grouping=True)
'$1.00'

I .. don't get it.i


On the production server, try

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

instead of

locale.setlocale( locale.LC_ALL, '' )

When you use '', the locale is set to the user's default (usually specified by the LANG environment variable). On the production server, that appears to be 'C', while as a test user it appears to be 'en_CA.UTF-8'.


I ran into a similar problem where I run Django app via PyCharm (JetBrain's IDEA 12 based IDE), it was getting the same issue of

Currency formatting is not possible using the 'C' locale.

where as it worked fine by then running python manage.py runserver would just work fine. After some digging I found a thread discussion about environment variable LC_ALL here

And it turned out if you edit the "Run Configration" and add an environment variable, it will work just fine. See screenshot below. Hope this helps others who encounter the same problem.

Problems trying to format currency with Python (Django)


http://docs.python.org/library/locale.html#locale.setlocale says that it is not thread-safe, which shouldnt be a problem running the dev server, but could cause you problems running it on a production server in a multi-threaded environment!


I know this is an old one, but I had this issue and I was able to keep using:

locale.setlocale( locale.LC_ALL, '' )

As I wanted to be able to run this code on a windows machine, linux machine and mac osx machine. The above line should work with a windows machine by default, it won't with a mac or linux machine. If you are running the production server with apache, you'll need to set up the user running the apache service with the locale you desire. To do this (in ubuntu at least) go to /etc/apache2/ and edit the "envvars" file. You'll see in there it has this line by default:

export LANG=C

That is the reason you're getting the error, change this to:

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

Restart apache and you should be right as rain.

If you aren't running apache and you're getting that error then you just need to update the .bash_profile or .profile of the user running the webserver or python app, add the above two lines to the bash profile and restart the terminal session, start up the server and voila.

Hope this helps someone.


I was having the same problem. It worked in the shell (manage.py shell), but not from the MVT. I had to use locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') because apparently, I didn't have the en_CA.UTF-8 locale. Note the en_US... as opposed to en_CA. Just wanted to add the answer because it caught me again after rebuilding and had to find the comment to get fix it.


I was getting the same error message using Djano, Nginx and uwsgi. To get the environment, as opposed to just Django, to use the correct locale, I had to add a line to wsgi.py:

wsgi.py

os.environ['LC_ALL'] = "en_GB.UTF-8"

(Note: in my case I was after British, not US, currency formatting so used _GB. Restarting the relevant services may be necessary for the change to take effect)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号