Commit b3f47207 authored by Daniel Klaffenbach's avatar Daniel Klaffenbach 🐍

date picker: Use `dateFormat` which matches the field's expected format

parent a073d8f0
Pipeline #31310 passed with stage
in 16 seconds
......@@ -20,16 +20,23 @@
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
</div>
</div>
<script type="text/javascript">
var datefield_datepicker_init;
if (datefield_datepicker_init != true) {
{% datefield_locale_include %}
}
//Only enable datepicker if the current locale is supported
if (datefield_datepicker_init == true) {
$(function() {$("#{{field.auto_id}}").datepicker({{datepicker_options}});});
} else {
console.warn('The current locale ({{LANGUAGE_CODE}}) is either not loaded or not supported by the datepicker.')
}
</script>
{% endif %}
\ No newline at end of file
<script type="text/javascript">
var datefield_datepicker_init;
if (datefield_datepicker_init != true) {
{% datefield_locale_include %}
}
// Only enable datepicker if the current locale is supported
if (datefield_datepicker_init == true) {
$(function() {
var local_options = {{datepicker_options}};
if (!("dateFormat" in local_options)) {
// Make sure the datepicker's dateFormat matches the date format expected by the form
local_options["dateFormat"] = "{% datefield_dateformat_for_field field.field %}";
}
$("#{{field.auto_id}}").datepicker(local_options);
});
} else {
console.warn('The current locale ({{LANGUAGE_CODE}}) is either not loaded or not supported by the datepicker.')
}
</script>
{% endif %}
......@@ -45,4 +45,32 @@ def datefield_locale_include():
pass
else:
return mark_safe(content + ' datefield_datepicker_init = true;')
return ""
\ No newline at end of file
return ""
@register.simple_tag
def datefield_dateformat_for_field(field):
"""
Returns the date format for JQueryUI's date picker which matches the field's expected input format.
:param field: A DateField instance
:type field: django.forms.fields.DateField
:return: A dateFormat for JQueryUI's datepicker, which matches the field's date format
:rtype: str
"""
# The widget's format takes precedence. Usually this is not defined.
if hasattr(field.widget, "format") and field.widget.format:
date_format = field.widget.format
else:
try:
# `input_formats` actually is a list. The first element in that list will be used by Django's form renderer
# when rendering date objects (such as default values), so we are also using it for the date picker.
date_format = field.input_formats[0]
except KeyError:
date_format = "%Y-%m-%d"
# JqueryUI expects a different format date syntax, so let's convert the most common things :(
  • In urz-www/tucal!142 wird ein neuer Datepicker (Barrierefrei und ohne jQuery) hinzugefügt. Den Datumsformat-Parser habe ich vom Alten übernommen, da ich auch eine jQuery Funktion zur Verfügung stelle, die dann mit Entfernung von jQuery-UI (Blogartikel folgt) im September die alte Funktionalität noch mit unterstützt. Eventuell willst du dir das ja mal anschauen @klada--tu-chemnitz.de, Fragen gern an mich 😄

Please register or sign in to reply
date_format = date_format.replace("%Y", "yy")
date_format = date_format.replace("%y", "y")
date_format = date_format.replace("%d", "dd")
date_format = date_format.replace("%m", "mm")
return date_format
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment