tags:

views:

318

answers:

3

Hi,

I'm using Django to serve up some XML. Part of the requirements is that my dates are formatted as follows: 1969-12-31T18:33:28-06:00

How can I output a DateField in Django in that format?

+1  A: 

Check out these docs:

http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date http://docs.djangoproject.com/en/dev/ref/settings/#setting-DATE_FORMAT http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now

This should be enough for you to format your date any way, you wish.

gruszczy
+1  A: 

If you look under date in the Django documentation you'll see that a variety of formats for the date are supported. Basically in a template you would do

{{ value|date "some format string" }} 

where the format string can be one of the ones defined under date on the page above or one of the custom ones defined under the now function (which includes ISO 8601 format) which is also on the page linked to above. I'm assuming that when you output to xml you do so in a similar way to a template for a normal web page.

Amos
Thanks. I solved it by using the folowing template in my xml: <EntryDate>{{ note.entry_date|date:"Y-m-d\TH:i:sO" }}</EntryDate>
rein
Oh, one important thing to note is that the field has to be of type DateTimeField (instead of DateField) or else the template simply swallows the value.
rein
The correct syntax is: {{ value|date:"DATETIME_FORMAT" }}
Emil Stenström
If he uses that format then he will get a date/time in the format: Feb. 4, 2003, 4 p.m which is not what the OP asked for (cf http://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#datetime-format )
Amos
+1  A: 

Since Django is written in Python,

http://docs.python.org/library/datetime.html#datetime.datetime.isoformat

Emyr