Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_class on label #39

Closed
kevinmickey opened this issue Jun 22, 2016 · 8 comments
Closed

add_class on label #39

kevinmickey opened this issue Jun 22, 2016 · 8 comments

Comments

@kevinmickey
Copy link

It would be nice to have add_class be able to accept field labels.

See https://bitbucket.org/kmike/django-widget-tweaks/issues/6/not-able-to-add-class-on-label

@luanfonceca
Copy link

+1

@luanfonceca
Copy link

luanfonceca commented Nov 24, 2016

Or just {{ form.field.label_tag|add_class='control-label' }} like the bootstrap requires.
The label_tag is a django method used to render the label.

Update:
I did this to make it work:

# Templatetags files
@register.filter("add_label_class")
@silence_without_field
def add_label_class(field, css_class):
    return field.label_tag(attrs={'class': css_class})

# html
{{ form.field|add_label_class:'control-label' }} 

This renders: <label for="id_field" class="control-label">Field</label>

@resurtm
Copy link

resurtm commented Oct 15, 2017

Another solution is to render <field></field> tag manually. Something like this:

<div class="form-group row">
    <label for="{{ form.make.auto_id }}" class="col-sm-3 col-form-label">{{ form.make.label }}</label>
    <div class="col-sm-9">
        {% render_field form.make class+='form-control' %}
        {% include 'common/partials/field-errors.html' with errors=form.make.errors %}
    </div>
</div>

@treyd
Copy link

treyd commented Dec 4, 2017

@resurtm I am using something similar to your solution... can you share what you're including with {% include 'common/partials/field-errors.html' with errors=form.make.errors %} ?

@resurtm
Copy link

resurtm commented Dec 4, 2017

@treyd, oh there is nothing special inside:

{% if errors %}
    <div class="invalid-feedback">
        {% for error in errors %}
            {{ error }}
        {% endfor %}
    </div>
{% endif %}

@camilonova
Copy link
Member

It was just merged.

@denizdogan
Copy link

@camilonova This is all great, any ideas on when the next release will be out? :) Would love to use this in a new project I'm working on.

@camilonova
Copy link
Member

I'll see if I can make a release real quick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants