How to get current language "short_date_format" in django?

I have successfully configured L10N on my project, and I can translate the dates into the correct format. But now I need SHORT_DATE_FORMAT for the current locale on my templates or in the context of SHORT_DATE_FORMAT .

Getting from django.conf.settings always gives me the default value, m/d/Y The locale is currently set to pt_BR , so the format should be d/m/Y

 In [42]: settings.LANGUAGE_CODE Out[42]: 'pt-br' In [43]: settings.USE_L10N Out[43]: True In [44]: settings.SHORT_DATE_FORMAT Out[44]: 'm/d/Y' 

Any clues?

By the way, I'm really trying to do this: get the current locale format so that I can pass it to the bootstrap-datepicker plugin. It currently uses m/d/Y , and django indicates the date in d/m/Y format.

+6
source share
2 answers

I was able to do this using babel :

settings.py :

 MIDDLEWARE_CLASSES = ( ... 'babeldjango.middleware.LocaleMiddleware', ... ) 

context_processors.py :

 def global_context(request): # Fixing the date format so bootstrap-datepicker understands. js_frm = request.locale.date_formats['medium'].pattern context = { ... return context 
0
source

I needed to do the following:

 from django.conf import settings from django.utils import formats correct_format = formats.get_format("SHORT_DATE_FORMAT", lang=settings.LANGUAGE_CODE) 
+2
source

Source: https://habr.com/ru/post/943588/


All Articles