Hello!
I am trying to get a default initial value to be displayed in my ModelForm.
I don’t really understand the official documentation…
Can somebody point me in the right direction… Thank in advance!
My Code:
Forms.Py
class DateInput(forms.DateInput):
input_type = 'date'
class TimeInput(forms.TimeInput):
t_input_type = 'time'
class CheckboxInput(forms.CheckboxInput):
c_input_type = 'checkbox'
class NumberInput(forms.NumberInput):
n_input_type = 'number'
class JournalForm(ModelForm):
class Meta:
model = PersonalJournal
fields = [
'date',
'time',
'person',
'food',
'amount',
'unit',
'drink',
'remark',
'image'
]
widgets = {
'date': DateInput(attrs={'id':'selectstylingDate'}),
'time': TimeInput(attrs={'id':'selectstylingTime'}),
'amount': NumberInput(attrs={'id':'selectstylingAmount'}),
'person': Select(attrs={'id':'selectstylingPerson'}),
'food': Select(attrs={'id':'selectstylingFood'}),
'unit': Select(attrs={'id':'selectstylingUnit'}),
'remark': Textarea(attrs={'cols':73,'rows': 2 , 'id':'selectstylingRemark','placeholder':'Opmerking', }),
'image': ClearableFileInput(attrs={'id':'selectstylingImage'})
}
Views.py
def journal_entry(request):
template = loader.get_template('journal_data/journal_entry.html')
initial_data = {
'remark':'test'
}
Journal_form = JournalForm(request.POST ,request.FILES,initial={'remark':'test'})
if Journal_form.is_valid():
Journal_form['remark'].value()
Journal_form.save()
Journal_form = JournalForm()
messages.success(request, f'Registratie aangemaakt!')
return HttpResponseRedirect('/')
data={
'Journal_form':Journal_form,
}
return HttpResponse(template.render(data, request))