Inside a Django app I have a Person model that I want to use in a form to capture both student and teacher data. The fields are identical in both forms, but the labels should be different. How can I change the labels before passing the form to the template?
The class for the form with default labels for student_form:
class ProfileForm(forms.ModelForm):
class Meta:
model = Person
fields = ('first_name', 'last_name', 'email')
labels = {
'first_name': label("Student's Name"),
'last_name': label("Student's Surname")
}
I tried to change the labels attributes as per below, but Django says that ProfileForm has no attribute 'labels'.
def add_profile(request):
teacher_form = ProfileForm(prefix = 'teacher')
teacher_form.labels['first_name'] = label("Teacher's Name")
teacher_form.labels['last_name'] = label("Teacher's Surname")
return render(request, "school/add_profile.html", {
"profileform" : teacher_form
})
Is there a way to access the Meta class to change its attributes?
You can change the labels like this:
teacher_form.fields['first_name'].label = "Teacher's Name"
teacher_form.fields['last_name'].label = "Teacher's Surname"