Django form: Allow user to insert a date in dd-mm-yyyy format and then convert in yyyy-mm-dd

2,154 views
Skip to first unread message

MarcoS

unread,
Mar 18, 2009, 6:43:38 AM3/18/09
to Django users
Hi all,
I'm implementing a form in django with a DateField field in forms.py.
My purpose is to allow a user to insert a date in european format (dd/
mm/yyyy) and then, obviously, convert it in the yyyy/mm/dd format to
let's django check if it's a valid date and save it in a db.

currently I've a javascript code that intercept the "submit command"
in the html form and converts the date in yyyy/mm/dd.

My ask is: It's possible to implement the convertion, not in a
javascript function, but directly in views.py or forms.py before
cleaning the date?

Adi Sieker

unread,
Mar 18, 2009, 6:53:19 AM3/18/09
to django...@googlegroups.com
Hi,

check the DateField form widget. It allows you to specify what format
the date should be in.

http://docs.djangoproject.com/en/dev/ref/forms/fields/#datefield

adi

MarcoS

unread,
Mar 18, 2009, 8:01:59 AM3/18/09
to Django users
Hi Adi, thanks for your reply!

I had just tried the DateField.input_formats, but the problem was that
I'm using the date admin widgets.
If I specify input_formats like: '%d.%m.%y', I can't use the admin
widgets 'cause it insert the date in the format: '%Y-%m-%d' (how you
can see in django/contrib/admin/media/js/admin/DateTimeShortcuts.js
line 239)...

koenb

unread,
Mar 18, 2009, 8:41:55 AM3/18/09
to Django users
On 18 mrt, 13:01, MarcoS <marc.se...@gmail.com> wrote:
> Hi Adi, thanks for your reply!
>
> I had just tried the DateField.input_formats, but the problem was that
> I'm using the date admin widgets.
> If I specify input_formats like: '%d.%m.%y', I can't use the admin
> widgets 'cause it insert the date in the format: '%Y-%m-%d' (how you
> can see in django/contrib/admin/media/js/admin/DateTimeShortcuts.js
> line 239)...
>

Marco,

look at my solution here: http://groups.google.be/group/django-users/msg/50eba033580c7536

to use the admin date picker, you need to tell your widget to add the
correct attrs so the last line becomes something like this:

super(DateWidget, self).__init__(format=settings.DATE_OUTPUT_FORMAT,
attrs={'class': 'vDateField'})

good luck,

Koen

Adi Sieker

unread,
Mar 18, 2009, 8:45:46 AM3/18/09
to django...@googlegroups.com
Hi,

On 18.03.2009, at 13:01, MarcoS wrote:


Hi Adi, thanks for your reply!

I had just tried the DateField.input_formats, but the problem was that
I'm using the date admin widgets.
If I specify input_formats like: '%d.%m.%y', I can't use the admin
widgets 'cause it insert the date in the format: '%Y-%m-%d' (how you
can see in django/contrib/admin/media/js/admin/DateTimeShortcuts.js
line 239)...

The Problem with the Admin is that the DateField doesn't respect any output formats.
Check this ticket for a possible solution: http://code.djangoproject.com/ticket/3672

adi

--
Adi J. Sieker         mobile: +49 - 178 - 88 5 88 13
Freelance developer   skype:  adijsieker
SAP-Consultant        web:    http://www.sieker.info/profile
                      openbc: https://www.openbc.com/hp/AdiJoerg_Sieker/



MarcoS

unread,
Mar 18, 2009, 3:48:05 PM3/18/09
to Django users
Thanks for your replies, Koeb and Ady.

Ady, if I understood I've to place the code from

http://code.djangoproject.com/attachment/ticket/3672/ticket%233672--patch-test.diff

to

- python/site-packages/django/forms/forms.py and
- python/site-packages/django/forms/widgets.py

and then I call the form with the follow field:

date = forms.DateField(widget=DateTextInput(format='d/m/y'),
input_formats=['%d/%m/%y'])

It's correct?

Adi Sieker

unread,
Mar 19, 2009, 3:32:52 PM3/19/09
to django...@googlegroups.com
On 18.03.2009, at 20:48, MarcoS wrote:


Thanks for your replies, Koeb and Ady.

Ady, if I understood I've to place the code from

http://code.djangoproject.com/attachment/ticket/3672/ticket%233672--patch-test.diff

to

- python/site-packages/django/forms/forms.py and
- python/site-packages/django/forms/widgets.py

Yes, basically. 


and then I call the form with the follow field:

date = forms.DateField(widget=DateTextInput(format='d/m/y'),
input_formats=['%d/%m/%y'])

It's correct?


In the diff file there are some regression tests which show you how to use the Fields.

adi
Reply all
Reply to author
Forward
0 new messages