How to use Datepicker in Django?

datepicker field in django

Below is my favorite way to use Datepicker, a very simple and clean way, because there is no need to include JQuery Datepicker.

In models.py

from django.db import models

class Offer(models.Model):
    expiration_date = models.DateField(null=True)

In forms.py

from django import forms
from django.forms import ModelForm
from .models import Offer

class DateInput(forms.DateInput):
    input_type = 'date'

class OfferForm(forms.ModelForm):
    class Meta:
        model = Offer
	fields = fields = '__all__'	
	widgets = {
            'expiration_date': DateInput(),
        }

In template

<form action="" enctype="multipart/form-data" method="POST">
{% csrf_token %}  
{{ form|crispy }}
<input type="submit" name="submit" value="Submit">
</form>

The result

The field look very nice!

django datepicker

I invite you to post in the comments your favorite way to use Datepicker in Django.

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Reply

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