2👍
✅
For your form, you can use django’s model form. You can add some definition to your model form with the class Meta
:
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = [
'name', 'enrollment_no', # etc...
]
widgets = {
'name': forms.TextInput( attrs={ 'class': 'form-control', 'placeholder': 'Title', 'required': True, } ),
'enrollment_no': # etc...
}
# you can also override validation/clean/save/etc methods here
*Note – because all of your fields are required (according to your model, and because we’re using a model form here), django will validate requiring all fields.
Then in your template (assuming you pass the form instance in the context to your template as form
) you can access each field like so:
...
...
<div class='col-md-11'>
{{ form.name }}
</div>
...
...
Source:stackexchange.com