How to set default date in SelectDateWidget in django

瘦欲@ 提交于 2019-12-12 10:49:12

问题


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(),         
      }

回答1:


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.




回答2:


There was similar problem here

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



来源:https://stackoverflow.com/questions/8709116/how-to-set-default-date-in-selectdatewidget-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!