How to use Datepicker 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.


2 thoughts on “How to use Datepicker in Django?”

  1. Thank You! After days of misdirection, incomplete and outdated solutions including a bout with JQuery and Bootstrap, it took less than 3 minutes to implement your solution!

    Reply

Leave a Comment

WebPedia.net