Django datetimefield不显示新的内联表单行的datetimepicker小部件 [英] Django datetimefield not showing datetimepicker widget for new inline form rows
问题描述
关于下图,当我单击添加条目"时,新条目行不再具有datetimepicker小部件.这很奇怪,因为它具有正确的类. +1字段代表django表单自动添加的附加字段.这似乎工作正常.只有页面呈现后添加的字段才可能是解决方案的关键.
我正在使用flatpickr软件包作为我的datetimepicker小部件.
trade_form.html
{% extends 'dashboard/base.html' %}
{% load static %}
{% block content %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'js/formset/jquery.formset.js' %}"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-3">
<h2>New Trade</h2>
</div>
<div class="row">
<div class="col">
<form action="" method="post">{% csrf_token %}
{{ form.media }}
{{ form.as_p }}
<table class="table">
{{ entries.management_form }}
{% for form in entries.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" value="Save"/> <a href="{% url 'trade-list' %}">back to the list</a>
</form>
</div>
</div>
<script type="text/javascript">
$('.formset_row').formset({
addText: 'add entry',
deleteText: 'remove',
prefix: 'entry_set'
});
</script>
<!-- Makes DateTimeFields Show Picker-->
<script>
// This code activates flatpickr on fields with the 'datetimefield' class when the document has loaded
window.addEventListener("DOMContentLoaded", function () {
flatpickr(".datetimefield", {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
});
</script>
{% endblock content %}
forms.py
class EntriesForm(ModelForm):
class Meta:
model = Entry
exclude = ()
widgets = {
'date': forms.DateTimeInput(format='%Y-%m-%d %H:%M:%S', attrs={'class': 'datetimefield'}),
}
# Adding default class to every visible field
def __init__(self, *args, **kwargs):
super(EntriesForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
if field.widget.attrs.get('class'):
field.widget.attrs['class'] += ' form-control'
else:
field.widget.attrs['class'] = 'form-control'
EntriesFormSet = inlineformset_factory(Trade, Entry, form=EntriesForm, extra=1)
更新了trade_form.html脚本,但仍然没有运气. 到目前为止,这段代码只是删除了已经起作用的字段上的datetimepicker小部件.因此,这可能只是格式问题.
<!-- Makes DateTimeFields Show Picker-->
<script>
// This code activates flatpickr on fields with the 'datetimefield' class when the document has loaded
window.addEventListener(
"DOMContentLoaded", function () {
flatpickr(".datetimefield", {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
}
"added", function() {
flatpickr(".datetimefield", {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
}
);
</script>
完成调用后,放置added
的位置在django-dynamic-formset
调用中,而不是在addEventListener
代码中通过django-dynamic-formset
本身.
那是:(注意:由于我目前正在通话中,我尚未对此进行测试)
<script type="text/javascript">
function applyFlatpickr(parent) {
flatpickr(parent.find(".datetimefield"), {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
}
<!-- Makes DateTimeFields Show Picker-->
// This code activates flatpickr on fields with the 'datetimefield' class when the document has loaded
window.addEventListener("DOMContentLoaded", function(){applyFlatpickr($(document))});
$('.formset_row').formset({
addText: 'add entry',
deleteText: 'remove',
prefix: 'entry_set',
added: applyFlatpickr
});
</script>
</script>
django-dynamic-formset
将新创建的行作为jQuery对象传递给added
函数,因此通过在参数上使用.find
,我们只能匹配新添加的行,并且仅对新行应用flatpickr.>
来自django-dynamic-formset
文档:
added
如果您将此设置为一个函数,则该函数将分别被调用 添加新表格的时间.该函数应采用单个参数,row
;它将传递一个jQuery对象,包装形式为 刚刚添加.)
In regards to the image below, when I click "add entry", the new entry row no longer has the datetimepicker widget. Which is odd because it has the right class. The +1 field represents the django form auto-added additional field. Which seems to work fine. It's only the fields that are added after the page has rendered which may be a key to the solution.
I am using the flatpickr package as my datetimepicker widget.
trade_form.html
{% extends 'dashboard/base.html' %}
{% load static %}
{% block content %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'js/formset/jquery.formset.js' %}"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-3">
<h2>New Trade</h2>
</div>
<div class="row">
<div class="col">
<form action="" method="post">{% csrf_token %}
{{ form.media }}
{{ form.as_p }}
<table class="table">
{{ entries.management_form }}
{% for form in entries.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" value="Save"/> <a href="{% url 'trade-list' %}">back to the list</a>
</form>
</div>
</div>
<script type="text/javascript">
$('.formset_row').formset({
addText: 'add entry',
deleteText: 'remove',
prefix: 'entry_set'
});
</script>
<!-- Makes DateTimeFields Show Picker-->
<script>
// This code activates flatpickr on fields with the 'datetimefield' class when the document has loaded
window.addEventListener("DOMContentLoaded", function () {
flatpickr(".datetimefield", {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
});
</script>
{% endblock content %}
forms.py
class EntriesForm(ModelForm):
class Meta:
model = Entry
exclude = ()
widgets = {
'date': forms.DateTimeInput(format='%Y-%m-%d %H:%M:%S', attrs={'class': 'datetimefield'}),
}
# Adding default class to every visible field
def __init__(self, *args, **kwargs):
super(EntriesForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
if field.widget.attrs.get('class'):
field.widget.attrs['class'] += ' form-control'
else:
field.widget.attrs['class'] = 'form-control'
EntriesFormSet = inlineformset_factory(Trade, Entry, form=EntriesForm, extra=1)
Updated trade_form.html scripts but with still no luck. So far this code just removed the datetimepicker widget on the fields that were already working. So it may just be a format issue.
<!-- Makes DateTimeFields Show Picker-->
<script>
// This code activates flatpickr on fields with the 'datetimefield' class when the document has loaded
window.addEventListener(
"DOMContentLoaded", function () {
flatpickr(".datetimefield", {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
}
"added", function() {
flatpickr(".datetimefield", {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
}
);
</script>
The place where the added
needs to be put is in the django-dynamic-formset
call, rather than in the addEventListener
code, as the call is done by django-dynamic-formset
itself.
That is: (note: I haven't tested this myself because I'm on phone at the moment)
<script type="text/javascript">
function applyFlatpickr(parent) {
flatpickr(parent.find(".datetimefield"), {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
});
}
<!-- Makes DateTimeFields Show Picker-->
// This code activates flatpickr on fields with the 'datetimefield' class when the document has loaded
window.addEventListener("DOMContentLoaded", function(){applyFlatpickr($(document))});
$('.formset_row').formset({
addText: 'add entry',
deleteText: 'remove',
prefix: 'entry_set',
added: applyFlatpickr
});
</script>
</script>
django-dynamic-formset
passes the newly created row to the added
function as a jQuery object, so by using .find
on the argument we can match only the newly added row and apply flatpickr only to that new row.
From the django-dynamic-formset
docs:
added
If you set this to a function, that function will be called each time a new form is added. The function should take a single argument,row
; it will be passed a jQuery object, wrapping the form that was just added.)
这篇关于Django datetimefield不显示新的内联表单行的datetimepicker小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!