Home » Django » django template date format

django template date format

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have dates in isoformat in the db, %Y-%m-%d however when the date is passed on to the template, it comes out as something like Oct. 16, 2011.

Is there anyway that I can manipulate the format to whatever I want?

Answers:

date

settings.DATE_FORMAT

Questions:
Answers:

Within your template, you can use Django’s date filter. E.g.:

<p>Birthday: {{ birthday|date:"M d, Y" }}</p>

Gives:

Birthday: Jan 29, 1983

More formatting examples in the date filter docs.

Questions:
Answers:

Set both DATE_FORMAT and USE_L10N

To make changes for the entire site in Django 1.4.1 add:

DATE_FORMAT = "Y-m-d"

to your settings.py file and edit:

USE_L10N = False

since l10n overrides DATE_FORMAT

This is documented at: https://docs.djangoproject.com/en/dev/ref/settings/#date-format

Questions:
Answers:

In order to change date format in the views.py and then assign it to template.

# get the object details 
home = Home.objects.get(home_id=homeid)

# get the start date
_startDate = home.home_startdate.strftime('%m/%d/%Y')

# assign it to template 
return render_to_response('showme.html' 
                                        {'home_startdate':_startDate},   
                                         context_instance=RequestContext(request) )  

Leave a Reply

Your email address will not be published. Required fields are marked *