Olivier Pons
Olivier Pons

Reputation: 15778

Django forms: add a custom label

I have a form that contains a lot of widget.

To summarize, a widget = a label + an input.

I have a big loop where I display my fields like this:

{% if field.label %}
    <label>{{ field.label }}</label>
    <label for="{{ field.auto_id }}" sr-only">{{ field.label }}
    </label>
{% endif %}
{{ field }}

My client want me to add a special text between two fields.

He wants something like:

I'm looking for a clean way to do this.

Upvotes: 0

Views: 1406

Answers (1)

Ian Price
Ian Price

Reputation: 7616

This is what the help_text attribute of a field is generally for. Think, for example, of a small caption that says "Two-letter abbreviation" below an input for a state/province.

...
{{ field }}
{% if field.help_text %}<div class='styleme'>{{ field.help_text }}</div>{% endif %}

Of course, you could always render each field individually if need be. Cleaner (if you have many fields) - test for when a certain field is in the loop.

{% if field.label == "A Certain Field" %}
    <div>Some text here</div>
{% endif %}

Upvotes: 1

Related Questions