Ace_Gentile
Ace_Gentile

Reputation: 244

Django: add a DEFAULT class into input/select fields

What I need to is just add a default class I'll call "myclass" to all input/select fields appearing into add/edit forms. I want to transform this

<div class="field-label">
<div>
<label for="id_label" class="required">Label:</label>
<input type="text" name="label" id="id_label" class="vTextField">
</div>
</div>

Into this

<div class="field-label">
<div>
<label for="id_label" class="required">Label:</label>
<input class="myclass" type="text" name="label" id="id_label" class="vTextField">
</div>
</div>

I know that this output is done into fieldset.html template (admin/includes/fieldset.html) but I have no clue where field.field parameter is set and how, since is clearly already formatted:

[... ]
    {% if field.is_checkbox %}
        {{ field.field }}{{ field.label_tag }}
    {% else %}
        {{ field.label_tag }}
        {% if field.is_readonly %}
            <p>{{ field.contents }}</p>
        {% else %}
            {{ field.field}}
        {% endif %}
    {% endif %}
[...]

Where can I edit that? Thanx in advance

Upvotes: 0

Views: 188

Answers (2)

Trent Brown
Trent Brown

Reputation: 31

It looks like you can replace {{field.field}} with something like this:

<input id="{{ field.id_for_label }}"
                     name="{{ field.html_name }}"
                     value="{{ field.value }}"
                    {% if field.field.max_length != None %}
                        maxlength="{{ field.field.max_length }}"
                    {% endif %}
                    {% if field.field.min_length != None %}
                        minlength="{{ field.field.min_length }}"
                    {% endif %}
                     >

This should allow you to put the class you want on your inputs =)

Upvotes: 1

zubhav
zubhav

Reputation: 1559

Don't think there is a way to set default classes for inputs from Django. You should have a look at django-crispy-forms.

It will let you customize your form straight from your Form class. So, in the template you only have to add {% crispy some_form %}

Upvotes: 1

Related Questions