PacktPublishing / Django-4-by-example

Django 4 by example (4th Edition) published by Packt
https://djangobyexample.com/
MIT License
799 stars 461 forks source link

On Line Shop #21

Closed PRCervera closed 1 year ago

PRCervera commented 1 year ago

Do you know why django's templates display prices and amounts starting with a dollar sign ?

like ... $ 12.096,00

Do you know how avoid this behaviour ?

Do you know how display the sign of base_currency ?

Thank you ?

zenx commented 1 year ago

The book does not cover defining a custom base currency or managing multiple currencies. The $ symbol is hardcoded in the templates (see for example https://github.com/PacktPublishing/Django-4-by-example/blob/main/Chapter11/myshop/shop/templates/shop/product/detail.html#L18).

However, you could define a BASE_CURRENCY setting for example and create a context processor to include it in your templates. For example:

settings.py

BASE_CURRENCY = 'EUR'
BASE_CURRENCY_SYMBOL = '€'

TEMPLATES = [
    {
        # ...
        'OPTIONS': {
            'context_processors': [
                'context_processors.base_currency',
                # ...
            ]
        }
    }
]

context_processors.py

from django.conf import settings

def base_currency(request):
    return {'base_currency': settings.BASE_CURRENCY,
            'base_currency_symbol': settings.BASE_CURRENCY_SYMBOL}

Templates:

{{ base_currency_symbol }} {{ product.price }}

You can use a single currency and let the client pay in any other currency with Stripe. If you want mange multiple currencies in your shop and display prices in the preferred currency of the client you can use django-currencies https://github.com/panosl/django-currencies

PRCervera commented 1 year ago

Thank you Antonio !!!