In this tutorial we are going to explore three date/datetime pickers options that you can easily use in a Django
project. We are going to explore how to do it manually first, then how to set up a custom widget and finally how to
use a third-party Django app with support to datetime pickers.
The implementation of a date picker is mostly done on the front-end.
The key part of the implementation is to assure Django will receive the date input value in the correct format, and
also that Django will be able to reproduce the format when rendering a form with initial data.
We can also use custom widgets to provide a deeper integration between the front-end and back-end and also to promote
better reuse throughout a project.
In the next sections we are going to explore following date pickers:
This is a great JavaScript library and it integrate well with Bootstrap 4. The downside is that it requires moment.js
and sort of need Font-Awesome for the icons.
It only make sense to use this library with you are already using Bootstrap 4 + jQuery, otherwise the list of CSS and JS
may look a little bit overwhelming.
To install it you can use their CDN or download the
latest release from their GitHub Releases page.
If you downloaded the code from the releases page, grab the processed code from the build/ folder.
Below, a static HTML example of the datepicker:
Direct Usage
The challenge now is to have this input snippet integrated with a Django form.
forms.py
template
The script tag can be placed anywhere because the snippet $(function () { ... }); will run the datetimepicker
initialization when the page is ready. The only requirement is that this script tag is placed after the jQuery script
tag.
Custom Widget
You can create the widget in any app you want, here I’m going to consider we have a Django app named core.
core/widgets.py
In the implementation above we generate a unique ID datetimepicker_id and also include it in the widget context.
Then the front-end implementation is done inside the widget HTML snippet.
widgets/bootstrap_datetimepicker.html
Note how we make use of the built-in django/forms/widgets/input.html template.
Now the usage:
core/forms.py
Now simply render the field:
template
The good thing about having the widget is that your form could have several date fields using the widget and you
could simply render the whole form like:
Below, a static example so you can see the minimum requirements and how all the pieces come together:
Direct Usage
A basic integration with Django would look like this:
forms.py
Simple form, default widget, nothing special.
Now using it on the template:
template
The id_date is the default ID Django generates for the form fields (id_ + name).
Custom Widget
core/widgets.py
widgets/xdsoft_datetimepicker.html
To have a more generic implementation, this time we are selecting the field to initialize the component using its name
instead of its id, should the user change the id prefix.
This is a very beautiful and minimalist date picker. Unfortunately there is no time support. But if you only need dates
this is a great choice.
To install this datepicker you can either use their CDN or download the
sources from their GitHub releases page. Please note that they
do not provide a compiled/processed JavaScript files. But you can download those to your local machine using the CDN.
Direct Usage
A basic integration with Django (note that we are now using DateField instead of DateTimeField):
forms.py
template
Custom Widget
core/widgets.py
widgets/fengyuanchen_datepicker.html
Usage:
core/forms.py
template
Conclusions
The implementation is very similar no matter what date/datetime picker you are using. Hopefully this tutorial provided
some insights on how to integrate this kind of frontend library to a Django project.
As always, the best source of information about each of those libraries are their official documentation.
I also created an example project to show the usage and implementation of the widgets for each of the libraries
presented in this tutorial. Grab the source code at github.com/sibtc/django-datetimepicker-example.