Reputation: 1926
I have following model form where I have to add a hidden field.
class AddEditGroupForm(forms.ModelForm):
id_sel_comp = forms.CharField(
label='selected company',
initial=0,
required=True,
widget=forms.HiddenInput(attrs={'id': 'id_sel_comp'})
)
class Meta:
model = Group
fields = ('name', 'id_sel_comp')
def __init__(self, *args, **kwargs):
super(AddEditGroupForm, self).__init__(*args, **kwargs)
self.fields['name'].widget.attrs.update({'class': 'form-control m-input form-control-sm'})
def as_two_col_layout(self):
return self._html_output(
normal_row='<div class="form-group m-form__group row"><label class="col-sm-3 col-form-label">%(label)s</label><div class="col-sm-9">%(field)s%(help_text)s</div></div>',
error_row='%s',
row_ender='',
help_text_html=' <span class="m-form__help">%s</span>',
errors_on_separate_row=True)
The form displays only the hidden form field and the 'name' charfield is not displayed. When I mark the field 'id_sel_comp' as NOT hidden, all fields are displayed. What is wrong with this? The form is rendered in the template with:
{{ form.as_two_col_layout }}
Upvotes: 1
Views: 311
Reputation: 1504
You did not specify your row_ender
properly. You are currently setting it to '' which isn't correct to what your specified as normal_row
. Your row_ender
in your case is </div></div>
. So your as_two_col_layout
becomes,
def as_two_col_layout(self):
return self._html_output(
normal_row='<div class="form-group m-form__group row">'
'<label class="col-sm-3 col-form-label">%(label)s</label>'
'<div class="col-sm-9">%(field)s%(help_text)s</div></div>',
error_row='%s',
row_ender='</div></div>',
help_text_html=' <span class="m-form__help">%s</span>',
errors_on_separate_row=True)
Hope this helps!
Upvotes: 1