On 05/03/10 01:30, NaMaK wrote:

class DateTest(forms.Form):
...     in_date=forms.DateTimeField(initial=datetime.datetime.now())
...

Well, note that the now() was executed when python first saw it, which
was just a bit after it first saw the class definition. Think about it - it does make sense.

But django allows initial on a form field to be a callable that will be called for a value at display time, so if that's suitable, you can do:

class DateTest(forms.Form):
    in_date=forms.DateTimeField(initial=datetime.now )

# note absence of () on 'now' as you want to pass in now itself,
# not call it.

This is covered in the docs, btw.
http://docs.djangoproject.com/en/1.1/ref/forms/fields/#initial
(towards the end of that section)

Note deferring to display time with a callable is slightly different to passing in a value at form construction time - another option is to pass an initial dict of whatever you want at form construction time, if you want it to be that (probably more relevant for cases other than now/now()...)

f = DateTest(initial=dict(in_date=datetime.now()))

(callables also work there too)





--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to