modelform DateField rendering as text

23 views
Skip to first unread message

Ruchit Bhatt

unread,
Jan 22, 2018, 10:33:46 AM1/22/18
to Django users
models.py
    class HumanUser(AbstractUser):
        birth_date
= models.DateField(null=True, blank=True, verbose_name=u"DOB")

forms.py
class profile_form(forms.ModelForm):

   
def __init__(self, *args, **kwargs):
        kwargs
.setdefault('label_suffix', '')
       
super(profile_form, self).__init__(*args, **kwargs)
       
for field in self.fields:
           
self.fields[field].widget.attrs.update({'class': 'form-control form-control-line', })

   
class Meta:
        model
= User
        fields
= [
           
'full_name',
           
'birth_date',
           
'mobile',
           
'bio',
           
'gender',
       
]


This is my code where birth_date type is showing as text instead of date in HTML..any idea why ?

<input type="text" name="birth_date" class="form-control form-control-line" id="id_birth_date" />



Costja Covtushenko

unread,
Jan 22, 2018, 9:00:49 PM1/22/18
to django...@googlegroups.com
Hi,

Do you mean that it should be rendered like:
<input type=“date” …/>?

If so check that documentation page.
It is said that DateInput renders as type=’text’.

If you are curios why it is done in that way, please ask at: django-d...@googlegroups.com

Regards,
Constantine C.

On Jan 22, 2018, at 10:33 AM, Ruchit Bhatt <ruchitinn...@gmail.com> wrote:

forms.ModelForm

Ruchit Bhatt

unread,
Jan 22, 2018, 10:57:36 PM1/22/18
to Django users
yes, it should be
<input type=“date” …/>

ok i will check that link

Ruchit Bhatt

unread,
Jan 23, 2018, 2:17:48 AM1/23/18
to Django users
Is there any bootstrap hack to fix this ??

Daniel Roseman

unread,
Jan 23, 2018, 5:03:10 AM1/23/18
to Django users
On Tuesday, 23 January 2018 07:17:48 UTC, Ruchit Bhatt wrote:
Is there any bootstrap hack to fix this ??

You don't need a hack, you can just set the type manually:

    self.fields['birth_date'].widget.attrs['type'] = 'date'

-- 
DR.

Ruchit Bhatt

unread,
Jan 23, 2018, 7:18:33 AM1/23/18
to Django users
Hi,i tried that and got this.
Double "type" (no override).

<input type="text" name="birth_date" id="example-date-input" class="form-control form-control-line" type="date" />

Ruchit Bhatt

unread,
Jan 23, 2018, 11:07:46 AM1/23/18
to Django users
solved

forms.DateInput.input_type="date"
forms.DateTimeInput.input_type="datetime-local" 
Reply all
Reply to author
Forward
0 new messages