问题
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>
回答1:
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.)
来源:https://stackoverflow.com/questions/60185270/django-datetimefield-not-showing-datetimepicker-widget-for-new-inline-form-rows