After the response around my last post I am trying to change my totally manual templates for forms to simple form where I use {{ person_form }} etcetera.
I inmediately have problems. The first is that the model field name is shown in the form instead of the real (translated) name.
class Person(models.model):
firstName = models.CharField(_('first name'), blank=True, max_length=60)
nameAffix = models.CharField(_('affix'), blank=True, max_length=6)
familyName = models.CharField(_('family name'), blank=True, max_length=60)
and the view:
def create_person(request):
field_order = ['firstName', 'nameAffix', 'familyName', 'gender', 'dateOfBirth']
if request.method == 'POST':
person_form = PersonModelForm(data = request.POST)
phonenumber_form_set = PhoneNumberFormSet(data = request.POST)
if person_form.is_valid() and phonenumber_form_set.is_valid():
person = person_form.save()
for form in phonenumber_form_set:
if form.cleaned_data and form.cleaned_data.get("telephoneNumber"):
phonenumber=form.save()
person.telephoneNumbers.add(phonenumber)
return redirect('person-list')
else:
person_form = PersonModelForm(data = request.POST)
phonenumber_form_set = PhoneNumberFormSet(data = request.POST)
else:
person_form = PersonModelForm()
phonenumber_form_set = PhoneNumberFormSet()
context = {'person_form': person_form,
'phonenumber_form_set': phonenumber_form_set}
return render(request,
template_name="MyNetwork\\person_form_test.html",
context=context)
And the tmplate:
{% extends 'main-template.html' %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content%}
<form method="POST">
{% csrf_token %}
{{ person_form | crispy}}
{{ phonenumber_form_set | crispy }}
<input type="submit" value="Submit">
</form>
{% endblock content %}
And the form:
class PersonModelForm(BaseModelForm):
# prefix="person"
firstName = forms.CharField(
widget = forms.TextInput(attrs=
{
#"label":"TEST",
"placeholder": _("First name"),
"autofocus": "autofocus",
"type": "text",
"size": 30, # width in characters
"maxLength": Person._meta.get_field('firstName').max_length,
}
))
nameAffix = forms.CharField(
widget = forms.TextInput(attrs=
{
"placeholder": _("affix"),
"type": "text",
"size": 5, # width in characters
"maxLength": Person._meta.get_field('nameAffix').max_length,
}
))
#Other fields omitted
field_order = ['firstName', 'nameAffix', 'familyName', 'gender', 'dateOfBirth']
class Meta:
model = Person
fields = "__all__"
(I left out a few models inbetween)
The base model:
class BaseModelForm(ModelForm):
extra_information = forms.CharField(
widget = forms.Textarea(attrs=
{
"placeholder": _('extra information'),
"type": "text",
"rows": 3,
"maxLength": BaseModel._meta.get_field('extra_information').max_length,
}
))
def __init__(self, *args, **kwargs):
super(BaseModelForm, self).__init__(*args, **kwargs)
self.fields['extra_information'].required = False
class Meta:
model = BaseModel
fields = "__all__"
I found that showing the fieldname instead of the real name can be caused by:
- Not using the Meta property refering to the corresponding model
- By using forms.form instead of forms.modelform
- By manually renaming the fields
But I think I got that correct.
I stil get

instead of affix
Can anybody tell me what I do wrong?

