1

I was wondering what's wrong with the form I made.

enter image description here

It's a Date Input text box where it automatically populates the current date.

Could someone point out what's wrong?

from datetime import date
today = date.today()
currentDate = today.strftime("%m/%d/%Y")



class DateInput(forms.DateInput):
    input_type = 'date'


class salesForm(forms.Form):

entryDateField = forms.DateField(label="Entry Date", widget=DateInput(attrs={'value': currentDate, 'readonly': 'readonly', 'class':'col-sm-8'}))

1 Answer 1

2

i think you should use initial attribute.

from datetime import date


def current_date():
    today = date.today()
    return today.strftime("%m/%d/%Y")

# in your form class
date = forms.DateField(initial=current_date)
Sign up to request clarification or add additional context in comments.

8 Comments

Hello @MojixCoder, Thanks for your input! But it still didn't work. the date input is still unpopulated with the current date.
Hey, please check out this link.
yeah, actually. It's working if i use your example. but the thing is when i combined the 'initial=current_date' to my existing code. nothing is happening.
I think there's something wrong with my 'widget=DateInput(attrs={'class':'col-sm-8'})' code...
Well don't use it inside your widget, use it on your DateField
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.