Why Django render DateField type as text instead of date ?

216 views
Skip to first unread message

Ruchit Bhatt

unread,
Jan 23, 2018, 7:28:53 AM1/23/18
to Django developers (Contributions to Django itself)
Hi folks,
Here i am eager to find out why django render model dateField type as text ??

<input type="text" ...>

instead of

<input type="date" ...>

Is there any alternative or inbuilt method to render datefield as date ?


This is my code

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',
       
]

HTML output

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


Collin Anderson

unread,
Jan 23, 2018, 10:21:00 AM1/23/18
to django-d...@googlegroups.com
Hi,


You can get type="date" like so on your form: birth_date = forms.DateField(attrs={'type': 'date'}, required=False)

Thanks,
Collin


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/caa35f1c-3a8e-4e45-954b-14876efdf8db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages