views:

198

answers:

2

Hello ! For formatting a date using date filter you must use the following format :

{{ my_date|date:"Y-m-d" }}

If you use strftime from the standard datetime, you have to use the following :

my_date.strftime("%Y-%m-%d")

So my question is ... isn't it ugly (I guess it is because of the % that is used also for tags, and therefore is escaped or something) ?

But that's not the main question ... I would like to use the same DATE_FORMAT parametrized in settings.py all over the project, but it therefore seems that I cannot ! Is there a work around (for example a filter that removes the % after the date has been formatted like {{ my_date|date|dream_filter }}, because if I just use DATE_FORMAT = "%Y-%m-%d" I got something like %2001-%6-%12)?

+1  A: 

While this may not be the "right" answer, I got around this by adding another variable to settings and using it all over the place. I have dates formatted in JavaScript, datetime, and Django templates, so I added all three.

TIME_ZONE = 'America/Phoenix'  
DATETIME_FORMAT = 'm-d-Y H:m:s T'
DATE_FORMAT = _('m-d-Y')       
JS_DATE_FORMAT = _('mm-dd-yy')
PERC_DATE_FORMAT = _('%m-%d-%Y')

I also run them through localization for our customers in Mexico who prefer a different format.

Jack M.
Thanks ! I did this to ... But I hoped there would be "THE right solution" :)
sebpiq
A: 

Not part of your main question but could be handy anyway. I found it mentioned in the docs that the date filter format is based on php's date format.

Densefog