Change date time picker widget
This commit is contained in:
parent
08eef6408f
commit
7d83efed7f
6 changed files with 21 additions and 46 deletions
|
@ -1,24 +1,19 @@
|
|||
from django import forms
|
||||
from django.templatetags.static import static
|
||||
from django.utils.html import format_html
|
||||
from django.forms.utils import flatatt
|
||||
|
||||
|
||||
class DateTimePickerWidget(forms.DateTimeInput):
|
||||
template_name = "widgets/datetimepicker.html"
|
||||
input_type = "datetime-local"
|
||||
|
||||
def get_context(self, name, value, attrs):
|
||||
datetimepicker_id = "datetimepicker_{name}".format(name=name)
|
||||
if attrs is None:
|
||||
attrs = dict()
|
||||
attrs["data-target"] = "#{id}".format(id=datetimepicker_id)
|
||||
attrs["class"] = "form-control datetimepicker-input"
|
||||
context = super().get_context(name, value, attrs)
|
||||
context["widget"]["datetimepicker_id"] = datetimepicker_id
|
||||
return context
|
||||
def render(self, name, value, attrs=None, renderer=None):
|
||||
if value is None:
|
||||
value = ""
|
||||
else:
|
||||
value = value.strftime("%Y-%m-%dT%H:%M")
|
||||
|
||||
@property
|
||||
def media(self):
|
||||
css_url = "/static/datetime-picker/datetimepicker.min.css"
|
||||
js_url = "/static/datetime-picker/datetimepicker.full.min.js"
|
||||
return forms.Media(
|
||||
js=[js_url],
|
||||
css={"screen": [css_url]},
|
||||
final_attrs = self.build_attrs(
|
||||
attrs, {"type": self.input_type, "name": name, "value": value}
|
||||
)
|
||||
return format_html("<input{}>", flatatt(final_attrs))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue