3

I'm trying to localize where I put my currency symbols in my django template (before vs after the number and spacing are both important). Example:

<span>{{ price }} {{ some_model.currency }}</span>

Where:

  • some_model is an object, with property currency which is a symbol eg "€" or "$" or "RMB".
  • In addition to the currency, I also know the locale some_model.locale.

In consulting the localization docs it seems that django can format times and numbers, but not currencies? I could write a custom filter {{ price | currency_filter }}, but how do I set (and subsequently get) the session locale in the filter code? Ideally, I'd like to be able to set session's locale upon login, much like you can set the language:

from django.utils.translation import LANGUAGE_SESSION_KEY
def login_view (request):
    ...
    request.session[LANGUAGE_SESSION_KEY] = some_model.locale

and then write some sort of template filter:

def currency_filter (number):
   #hopefully some inbuilt session method to get the locale, then:
   if locale == 'en-US':
       return '$' + number
   #etc

Is this possible?

Escher
  • 5,418
  • 12
  • 54
  • 101
  • related? http://stackoverflow.com/questions/320929/currency-formatting-in-python – dani herrera May 20 '16 at 15:48
  • I read that, but I want the locale to be set for the session, not for the entire python environment. There will be many concurrent sessions; I can't keep changing the global python locale. – Escher May 20 '16 at 15:54
  • What about apply change rate and taxes? I mean, is not enough just to change currency simbol. – dani herrera May 20 '16 at 16:00
  • In my particular circumstance, it is sufficient to change just the symbol. Taxes and exchange rates would form part of separate calculations. – Escher May 20 '16 at 16:01

0 Answers0