7

I am using SelectDateWidget widget for entering date in the form field. But I want it to show the current date by default. How can I do that?

model.py

bdate = models.DateField(default=datetime.date.today())

This is giving error. can anyone tell the correct way to do that?

Also, my in template

{{ form.bdate }}

when I use the above mentioned line in my template it displays like this -- -- -- but I want something like this month date year. how can i do this?

My form is:

widgets = {
      'bdate' : SelectDateWidget(),         
      }
oz123
  • 27,559
  • 27
  • 125
  • 187
G Gill
  • 1,087
  • 1
  • 12
  • 24

2 Answers2

7

Using Django 1.3 the following worked for me:

join_date = forms.DateField(widget=SelectDateWidget(), label='Joining Date', initial=timezone.now())

The keyword is initial not default.

oz123
  • 27,559
  • 27
  • 125
  • 187
1

There was similar problem here

Try to use default=datetime.date.today or auto_now_add=True

Dharman
  • 30,962
  • 25
  • 85
  • 135
szaman
  • 6,666
  • 13
  • 53
  • 81
  • `auto_now_add=True` will have the result of automatically setting the date to now on save, *but* it won't make the actual field display anything by default. If fact, it really only makes sense to use it in conjunction with `editable=False`. – Chris Pratt Jan 03 '12 at 15:57