Set a css class and render the form in Django

Asked

Viewed 75 times

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 in all fields of the form the class 'form-control', and to involve my fields I created this method based on the "as_p" method of the class "Forms".

So when putting in my template:

{{ form.renderFormGroup }}

It renders the cute as I want, with one exception, I couldn’t set a class on my label.

I found some solutions on the internet to do 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 didn’t want to do this, I wanted to render straight from my class, putting only "form.renderFormGroup".

At first I tried to change the string of "_html_output", but the "label" of this string is different from the templante "label", while in "label" template is only label text in the "_html_output" string the "label" is the full tag, then I tried to set the values in the method "label_tag", I did a built-in for grabbing all boundfield and tried to set again by modifying the "label_tag", but did not change nothingness.

1 answer

0


I spent about two days chasing after this answer, I did amazing things in my code to try to solve this problem, and in the end find that it is something super simple, just put the following line in the constructor:

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

Then all the classes you assign to "required_css_class" are rendered in the label, below is the complete class:

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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.