Set a css class and render the form in django

0

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.

    
asked by anonymous 04.08.2018 / 04:59

1 answer

0

I've been running for a couple of days running through these answers, I've done some crazy things in my code to try to solve this problem, and in the end find out it's super simple, just put the following line in the constructor:

self.required_css_class = 'control-label is-visible'

Then all the classes that you assign to "required_css_class" are rendered in the label, follow the whole class below:

from django import forms

class FormDefault(forms.Form):
    def __init__(self, *args, **kwargs):
        super(FormDefault, self).__init__(*args, **kwargs)
        self.required_css_class = 'control-label is-visible'
        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)s %(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)
    
04.08.2018 / 05:07