Skip to main content

Flatpickr based DatePickerInput, TimePickerInput and DateTimePickerInput with date-range-picker functionality for django >= 2.0

Project description

This django widget contains Date-Picker, Time-Picker, DateTime-Picker input widgets with date-range-picker functionality for django version >= 2.0. The widget implements flatpickr to display date-pickers in django model forms and custom forms which can be configured easily for date-range selection. For Bootstrap date-picker see django-bootstrap-datepicker-plus.

Build Status Coverage Status Python Versions DJango Versions
Flatpickr Red Theme Flatpickr Default Theme Flatpickr Dark Theme

Demo

Getting Started

Prerequisites

  • Python >= 3.8

  • Django >= 2.0

Installing

Install the PyPI package via pip.

pip install django-flatpickr

Add django_flatpickr to INSTALLED_APPS in your settings.py file.

INSTALLED_APPS = [
    # Add the following
    "django_flatpickr",
]

Usage

The HTML template must have the following to render the flatpickr widget. A better example is here.

<!-- File: myapp/custom-form.html -->
{{ form.media }}  {# Adds all flatpickr JS/CSS files from CDN #}
{{ form.as_p }}   {# Renders the form #}

<form method="post">
  {% csrf_token %}
  {% bootstrap_form form %}
</form>

You can use it with generic views without a model form. It can also be used with custom forms and model forms as below.

Usage in Custom Form

# File: forms.py
from django_flatpickr.widgets import DatePickerInput, TimePickerInput, DateTimePickerInput
from .models import Event
from django import forms

class ToDoForm(forms.Form):
    todo = forms.CharField(widget=forms.TextInput())
    date = forms.DateField(widget=DatePickerInput())
    time = forms.TimeField(widget=TimePickerInput())
    datetime = forms.DateTimeField(widget=DateTimePickerInput())


# File: views.py
class CustomFormView(generic.FormView):
    template_name = "myapp/custom-form.html"
    form_class = ToDoForm

See models.py, forms.py, views.py, custom-form.html for more details.

Usage in Model Form

# File: forms.py
from django_flatpickr.widgets import DatePickerInput, TimePickerInput, DateTimePickerInput
from .models import Event
from django import forms

class EventForm(forms.ModelForm):
    class Meta:
        model = Event
        fields = ["name", "start_date", "start_time", "start_datetime"]
        widgets = {
            "start_date": DatePickerInput(),
            "start_time": TimePickerInput(),
            "start_datetime": DateTimePickerInput(),
        }


# File: views.py
class UpdateView(generic.edit.UpdateView):
    model = Event
    form_class = EventForm

See models.py, forms.py, views.py, event_form.html for more details.

Implement date-range-picker

DatePickers can be linked together to select a date-range, time-range or date-time-range without writing a single line of JavaScript.

# File: forms.py
from django_flatpickr.widgets import DatePickerInput, TimePickerInput
from django import forms

class EventForm(forms.ModelForm):
    class Meta:
        model = Event
        fields = ["name", "start_date", "end_date", "start_time", "end_time"]
        widgets = {
            "start_date": DatePickerInput(),
            "end_date": DatePickerInput(range_from="start_date"),
            "start_time": TimePickerInput(),
            "end_time": TimePickerInput(range_from="start_time"),
        }

Customization

To customize the look and features of flatpickr widget copy the settings block to your settings.py file and customize it. Settings applies globally to all flatpickr widgets used in your site.

You can set date and event hook options using JavaScript.

window.djangoFlatpickrOptions = {
    onChange: function (selectedDates) { console.log(selectedDates) }
}

Customize single input

from django_flatpickr.schemas import FlatpickrOptions

class ToDoForm(forms.Form):
    todo = forms.CharField(widget=forms.TextInput())
    start_date = forms.DateField(widget=DatePickerInput(
        attrs = {"class": "my-custom-class"}, # input element attributes
        options=FlatpickrOptions(altFormat="m/d/Y"),
    ))

Similarly set date and event hook options using JavaScript.

window.djangoFlatpickrOptions_start_date = {
    onChange: function (selectedDates) { console.log(selectedDates) }
}

Localization

Use locale option, see available localization options.

License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_flatpickr-2.0.3.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

django_flatpickr-2.0.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file django_flatpickr-2.0.3.tar.gz.

File metadata

  • Download URL: django_flatpickr-2.0.3.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for django_flatpickr-2.0.3.tar.gz
Algorithm Hash digest
SHA256 48f8fac131e8a1a512492277bd0b548fab10696c0a52ed58cab53c59d86e8f20
MD5 913abb6348223fbd68f1cedcea0355e5
BLAKE2b-256 1de0fcb8e190e862f24d7fcc658ab7384967ad4bed56343748043877a300122e

See more details on using hashes here.

File details

Details for the file django_flatpickr-2.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_flatpickr-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c1c82d735c76a7d2381d9e5d5b29b1c1d21f139798542e38eeaa4d3a70678d99
MD5 ae9a606bff89075e351b217a009801d8
BLAKE2b-256 f703efe882e8d3556b56b7f79f5cf8428691295a4a6816c9438f1dc6ef5de5b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page