Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add widgets to make use of HTML5's date/time picker #477

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
58 changes: 58 additions & 0 deletions docs/widgets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,61 @@ This renders a form ChoiceField as a Bootstrap 4 button group in the `primary` B
choices=((1, 'Vinyl'), (2, 'Compact Disc')),
initial=1,
)



DateTimeWidget
~~~~~~~~~~~~~~~~~~~~~~

This renders a form DateTimeField as a input of type datetime-local.

.. code:: django

from bootstrap4.widgets import DateTimeWidget

class MyForm(forms.Form):
media_type = forms.DateTimeField(
help_text="Pick a Date and Time",
required=True,
label="Schedule:",
widget=DateTimeWidget,
initial=datetime.now(),
)


DateWidget
~~~~~~~~~~~~~~~~~~~~~~

This renders a form DateField as a input of type date.

.. code:: django

from bootstrap4.widgets import DateWidget

class MyForm(forms.Form):
media_type = forms.DateField(
help_text="Enter your bird day",
required=True,
label="Bird Day:",
widget=DateWidget,
initial=date.today(),
)


TimeWidget
~~~~~~~~~~~~~~~~~~~~~~

This renders a form TimeField as a input of type time.

.. code:: django

from bootstrap4.widgets import TimeWidget

class MyForm(forms.Form):
media_type = forms.TimeField(
help_text="Pick a time:",
required=True,
label="Time:",
widget=TimeWidget,
initial=datetime.now().time(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type="datetime-local" class="{{ widget.attrs.class|default:'form-control' }}" name="{{ widget.name }}" id="{{ widget.attrs.id }}" value="{{ widget.value|default:'' }}"{% if widget.required %} required{% endif %}/>
32 changes: 31 additions & 1 deletion src/bootstrap4/widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from django.forms import RadioSelect
from django.forms import (
RadioSelect,
DateInput,
DateTimeInput,
TimeInput
)


class RadioSelectButtonGroup(RadioSelect):
Expand All @@ -9,3 +14,28 @@ class RadioSelectButtonGroup(RadioSelect):
"""

template_name = "bootstrap4/widgets/radio_select_button_group.html"


class DateWidget(DateInput):
"""This widget renders a HML5 date field type and enables date picker."""

input_type = "date"

def format_value(self, value):
return str(value or '')


class DateTimeWidget(DateTimeInput):
"""This widget renders a HML5 datetime-local field type and enables date and time picker."""

input_type = "datetime-local"
template_name = "bootstrap4/widgets/datetime_field.html"

def format_value(self, value):
return 'T'.join(str(value or '').split())


class TimeWidget(TimeInput):
"""This widget renders a HML5 time field type and enables time picker."""

input_type = "time"