CurrentDate 变量未在输入框中显示为值

问题描述

我想知道我制作的表格有什么问题。

enter image description here

这是一个日期输入文本框,它会自动填充当前日期。

有人能指出哪里出了问题吗?

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'}))

解决方法

我认为您应该使用 initial 属性。

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)