Answer the question
In order to leave comments, you need to log in
Why is the date passed but not displayed when passing an object to an instance ModelForm?
I am making a form for editing a database object, I want to make it so that when the page with the form is opened, it already contains data. I pass the required object to instance, but the values are displayed only in the NumberField, Select and TextInput fields.
At the same time, in the debugger, when the form is initialized and in return, the values \u200b\u200bof all fields are filled.
I suspect that this is a matter of bootstrap styles, or maybe not.
Who has already encountered such a problem, tell me what to do?
forms.py
class EditPriceForm(forms.ModelForm):
class Meta:
model = PriceList
fields = [
'dateBegin',
'dateEnd',
'cena',
'flCancel',
'idVallet'
]
widgets = {
'idVallet': forms.Select(attrs={
'class': 'form-control'
}),
'dateBegin': forms.DateInput(attrs={
'class': 'form-control',
'type': 'date',
'data-inputmask-alias': 'datetime',
'data-inputmask-inputformat': 'dd/mm/yyyy',
'data-mask': '',
}),
'dateEnd': forms.DateInput(attrs={
'class': 'form-control',
'type': 'date',
'data-inputmask-alias': 'datetime',
'data-inputmask-inputformat': 'dd/mm/yyyy',
'data-mask': '',
}),
}
def __init__(self, *args, **kwargs):
super(EditPriceForm, self).__init__(*args, **kwargs)
self.fields['cena'].widget = forms.NumberInput(attrs={'class': 'form-control'})
self.fields['flCancel'].widget = forms.CheckboxInput(attrs={'class': 'form-control'})
@login_required
def EditPriceView(request, objId):
if request.method == 'POST':
form = EditPriceForm(request.POST)
if form.is_valid():
obj = PriceList.objects.get(id=objId)
obj.cena = form.cleaned_data['cena']
obj.dateBegin = form.cleaned_data['dateBegin']
obj.dateEnd = form.cleaned_data['dateEnd']
obj.flCancel = form.cleaned_data['flCancel']
obj.idVallet = form.cleaned_data['idVallet']
obj.summaRub = obj.cena + ((obj.cena/100)*2)
obj.save()
return redirect('/base/edit_nomenkl/'+str(obj.idNomenkl.id))
if request.method == 'GET':
form = EditPriceForm(instance=PriceList.objects.get(id=objId))
return render(request, 'editPrice.html', {
'form': form,
'objId': objId
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question