Change date time picker widget

This commit is contained in:
cuom1999 2024-04-12 22:26:17 -05:00
parent 08eef6408f
commit 7d83efed7f
6 changed files with 21 additions and 46 deletions

View file

@ -1,24 +1,19 @@
from django import forms 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): class DateTimePickerWidget(forms.DateTimeInput):
template_name = "widgets/datetimepicker.html" input_type = "datetime-local"
def get_context(self, name, value, attrs): def render(self, name, value, attrs=None, renderer=None):
datetimepicker_id = "datetimepicker_{name}".format(name=name) if value is None:
if attrs is None: value = ""
attrs = dict() else:
attrs["data-target"] = "#{id}".format(id=datetimepicker_id) value = value.strftime("%Y-%m-%dT%H:%M")
attrs["class"] = "form-control datetimepicker-input"
context = super().get_context(name, value, attrs)
context["widget"]["datetimepicker_id"] = datetimepicker_id
return context
@property final_attrs = self.build_attrs(
def media(self): attrs, {"type": self.input_type, "name": name, "value": value}
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]},
) )
return format_html("<input{}>", flatatt(final_attrs))

View file

@ -262,6 +262,13 @@
border-radius: 1em; border-radius: 1em;
} }
.post-content-header {
margin-left: 0;
display: inline-flex;
align-items: center;
gap: 0.2em;
}
@media (max-width: 799px) { @media (max-width: 799px) {
.blog-sidebar, .blog-sidebar,

View file

@ -143,7 +143,7 @@
} }
input { input {
&[type=text], &[type=password], &[type=email], &[type=number] { &[type=text], &[type=password], &[type=email], &[type=number], &[type=datetime-local] {
padding: 4px 8px; padding: 4px 8px;
color: #555; color: #555;
background: #FFF none; background: #FFF none;

View file

@ -1,7 +1,7 @@
{% for post in posts%} {% for post in posts%}
<section class="{% if post.sticky %}sticky {% endif %}blog-box"> <section class="{% if post.sticky %}sticky {% endif %}blog-box">
<div style="margin-bottom: 0.5em"> <div style="margin-bottom: 0.5em">
<span class="item-header time"> <span class="post-content-header time">
{% with authors=post.authors.all() %} {% with authors=post.authors.all() %}
{%- if authors -%} {%- if authors -%}
<span class="user-img" style="width: 1.5em; height: 1.5em"> <span class="user-img" style="width: 1.5em; height: 1.5em">

View file

@ -7,12 +7,6 @@
clear: both; clear: both;
} }
} }
.item-header {
margin-left: 0;
display: inline-flex;
align-items: center;
gap: 0.2em;
}
.no-clarifications-message { .no-clarifications-message {
font-style: italic; font-style: italic;

View file

@ -1,21 +0,0 @@
<input
autocomplete="off"
type="{{ widget.type }}"
name="{{ widget.name }}"
{% if widget.value != None %}
value="{{ widget.value }}"
{% endif %}
{% for name, value in widget.attrs.items() %}
{% if value %}
{{ name }}{% if not value %}="{{ value }}"{% endif %}
{% endif %}
{% endfor %}
/>
<script>
$(function () {
$("input[name='{{ widget.name }}']").datetimepicker({
format: 'Y-m-d H:i:s',
});
});
</script>