How to set default date in SelectDateWidget in django

I use the SelectDateWidget widget to enter a date in the form field. But I want her to show the current date by default. How can i do this?

# modely.py

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

This gives an error. can anyone tell the right way to do this?

Also my template

 {{ form.bdate }} 

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

My form:

 widgets = { 'bdate' : SelectDateWidget(), } 
+4
source share
2 answers

Using Django 1.3, the following worked for me:

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

Keyword initial not default .

+2
source

There was a similar problem here

Try using default=datetime.date.today or auto_now_add=True

0
source

Source: https://habr.com/ru/post/1389025/


All Articles