I created the following class:
from django import forms
class FormDefault(forms.Form):
def __init__(self, *args, **kwargs):
super(FormDefault, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
def renderFormGroup(self):
return self._html_output(
normal_row='<div class="form-group form-group-material" %(html_class_attr)s><label class ="control-label is-visible">%(label)s</label> %(field)s%(help_text)s</div>',
error_row='%s',
row_ender='</div>',
help_text_html=' <span class="helptext">%s</span>',
errors_on_separate_row=True)
In my constructor I can set the 'form-control' class in all form fields, and to wrap my fields I created this method based on the 'as_p' method of the 'forms' class.
So when I put it in my template:
{{ form.renderFormGroup }}
It renders the form as I want, with one exception, I could not set a class on my label.
I found some solutions on the internet to make a for in the template:
{% for field in form %}
<div class="form-group form-group-material">
<label class="control-label is-visible">{{ field.label }}</label>
{{ field }}
{{ field.errors }}
{% if field.help_text %}
<span class="help-block">{{ field.help_text|safe }}</span>
{% endif %}
</div>
{% endfor %}
But I did not want to do that, I wanted to render directly from my class, just putting "form.renderFormGroup".
At first I tried to change the "_html_output" string, but the "label" of this string is different from the label of the templante, while in the template the "label" is only the text of the label, in the string of "_html_output" the "label" is the complete tag, then I tried to set the values in the method "label_tag", I made a for in the constructor grabbing all the boundfield and I tried again by modifying the "label_tag", but it did not change nothing.