[Answered ]-How to customize Date and Time field in django forms?

1👍

—— form.py ———

class new_album_form(forms.ModelForm):
    class Meta:
        model = album
        fields = ['album_name', 'album_artist', 'released_at', 'price', 'is_approved']

         widgets = {
            'released_at':forms.TextInput(attrs={'type':'datetime-local'}),
        }

—— Output ——–

enter image description here

0👍

According to your expected out, I think you have to add widgets to your form.

try this:

class new_album_form(forms.ModelForm):
    class Meta:
        model = album
        fields = ['album_name', 'album_artist', 'released_at', 'price', 'is_approved']
        widgets = {
               'album_name': forms.TextInput(attrs={'class':'form- 
                control'}),
               'album_artist': forms.TextInput(attrs={'class':'form- 
                control'}),
               'release_at': forms.DateTimeInput(attrs={'class':'form- 
                control', 'data-target': '#datetimepicker1'}),
               #Add your remaining widgets here
        }

And in templates:

<input type="date" data-target="#datetimepicker1" class="form-control" value="{{form.release_at}}" />  #form is a context name from views.py file

Leave a comment