How to use Datepicker in Django?

5/5 - (3 votes)

Regarding the question “How to use DatePicker in Django forms?”, 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

6 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
  2. This is run successfully but did not get a date picker on the page. only shows the submit button on the page. Could you please help to figure it out?

    Reply

Leave a Comment

WebPedia.net