DateField Form Dynamic initial value stuck

Hi hello to all!

I have this date pick form that allow the user to pick a date, send it to the server and then update the data in the dashboard.

forms.py

# update saldo refence date
class SaldoRefDate(forms.Form):
    refDate = forms.DateField(
        label='Ref. Date',
        required=True,
        widget=forms.DateInput(format="%Y-%m-%d", attrs={'type': 'date'}),
        input_formats=["%Y-%m-%d"],
        initial= datetime.now().strftime("%Y-%m-%d")
    )

Turns out that the initial date does not change dinamicaly, it stay’s stuck at the server’s deploy date.
I’ve tryed to use some JQuery to force the actual date on page load but, when the user change to any other date causes the page to reload and then, jQquery does his magic again and we ended stuck in todays date…

Is there any solution to this?

Thanks in advance.

Welcome @sylvioCampos !

Yep, that’s to be expected here. What you need to pass as the initial is a callable, and not an expression that is going to be evaluated when the field is defined. See the docs for initial.

The reason the page refreshes is because you made it work with js or jquery when you selected the date.

Hi Ken, thanks for the answer!

I think that this should do the job:

I’ll try in production and let you know.

Regards,

Sylvio Campos Neto