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

Issue

How to customzie the default dateTime field in django forms to be more friendly to the user.

forms.py:

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

and I am using the default django forms

<form method="post" novalidate>
  {% csrf_token %} 
  {{form.as_p}}
  <button type="submit" value="submit">Create</button>
</form>

My field output in the form

I want it to be like this

Solution

—— 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

Answered By – Mahammadhusain kadiwala

Answer Checked By – Mary Flores (FixeMe Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *