I created a button linked with a jquery
so that when I click on the edit button
in my form, the data from the entry will be populated into the form as well.
The rest of the values do get populated into the form via jquery
, however, my problem is with the dates
value. Somehow, the dates
are not being rendered into the form when passed back from jquery
.
html
...
<button type="button" class="edit-current-policy-term float-right"
data-toggle="modal" data-target="#edit-term-policy-modal"
data-term="{{ policy_period.term }}"
data-start_term="{{ policy_period.start_term }}"
data-end_term="{{ policy_period.end_term }}"
data-pk="{{ policy_period.pk }}">
<i class="fas fa-edit"></i>
</button>
jquery
$(document).ready(function () {
$(document).on('show.bs.modal', '#edit-term-policy-modal', function (event) {
const button = $(event.relatedTarget);
let term = button.data('term');
let start_term = button.data('start_term');
let end_term = button.data('end_term');
const pk = button.data('pk');
const modal = $(this);
modal.find('.modal-body #id_term_number').val(term);
modal.find('.modal-body #id_start_term').val(start_term);
modal.find('.modal-body #id_end_term').val(end_term);
});
});
forms
class NewTermPolicyPeriodForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(NewTermPolicyPeriodForm, self).__init__(*args, **kwargs)
self.fields['term'].widget = forms.TextInput(
attrs={'class': 'form-control', 'id': 'id_term_number', 'name': 'term_number', 'type': 'number',
'placeholder': 'Term number'})
self.fields['term'].required = True
self.fields['start_term'].widget = forms.DateInput(
attrs={'class': 'form-control', 'id': 'id_start_term', 'name': 'start_term', 'type': 'date',
'placeholder': 'Term start date'})
self.fields['start_term'].required = True
self.fields['end_term'].widget = forms.DateInput(
attrs={'class': 'form-control', 'id': 'id_end_term', 'name': 'end_term', 'type': 'date',
'placeholder': 'Term end date'})
self.fields['end_term'].required = True
self.fields['term_end'].widget = forms.CheckboxInput(
attrs={'class': 'form-control', 'id': 'id_term_end', 'name': 'term_end'})
self.fields['term_end'].required = False
class Meta:
model = TermPolicyPeriod
fields = [
'term', 'start_term', 'end_term', 'term_end'
]
Any thoughts?