user1070019
user1070019

Reputation: 121

Access form field attributes in templated Django

I've been doing some custom forms with django but I don't get how to access attributes that a specific form field has attached via the forms.py.

def putErrorInTitle (cls):
init = cls.__init__

def __init__ (self, *args, **kwargs):
    init(self, *args, **kwargs)
    if self.errors:
        for field_error in self.errors:
            self.fields[field_error].widget.attrs['title'] = self.errors[field_error][0]
            self.fields[field_error].widget.attrs['class'] = "help_text error_field"

cls.__init__ = __init__
return cls

That's how I attached the attibutes to the field.

<dl class="clearfix two">
 <dd>
  <label for="id_diagnosis">Diagnostico:</label>
   <select class="{{form.id_diagnosis.class}}" id="id_equipment_activity-{{ forloop.counter0 }}-id_diagnosis" name="equipment_activity-{{ forloop.counter0 }}-id_diagnosis">
    {% for x,y in form.fields.id_diagnosis.choices %}
    <option value="{{ x }}" {% ifequal form.id_diagnosis.data|floatformat x|floatformat %}selected="selected"{% endifequal %}>{{ y }}</option>
    {% endfor %}
    <option value="1000" {% ifequal form.id_diagnosis.data|floatformat '1000'|floatformat %}selected="selected"{% endifequal %}>Otro</option>
   </select>
 </dd>
 <dd class="vertical_center" id="optional_diagnosis"><label for="optional_diagnosis">Diagnostico opcional:</label>{{ form.optional_diagnosis }}</dd>
</dl>

I've been trying to access its attributes:

class="{{form.id_diagnosis.class}}", class="{{form.id_diagnosis.widget.class}}"

And I don't seem to find clear documentation about what's accessible and what's not. Really I would rather have old fashion documentation than django "friendly" one

Upvotes: 11

Views: 16381

Answers (3)

Koushik Das
Koushik Das

Reputation: 10793

The above answers are correct, however, I'd like to add a note for those who are accessing form fields in a loop.

If you're doing this in a loop like this

{% for field in form %}
    {{ field.field.widget.attrs.placeholder }} # field.field is the key here
{% endfor %}

Upvotes: 2

ianar&#233;
ianar&#233;

Reputation: 3298

In other cases it can can be useful to set and get field attributes.

Setting in form's init function:

self.fields['some_field'].widget.attrs['readonly'] = True

... and accessing it in a template:

{{ form.some_field.field.widget.attrs.readonly }}

Upvotes: 16

user1472268
user1472268

Reputation:

It looks like you just want to display form errors for each field. After the form is cleaned or validated in the view, the fields should contain the error messages. So that you can display them in the template like so:

<form action='.' method='post'>
    ...
    <div class='a-field'>
       {{ form.field_1.errors|join:", " }}
       {{ form.field_1.label_tag }}
       {{ form.field_1 }}
    </div>
    ...
</form>

If however you really want to display the form field attributes then you can try something like:

{{ form.field_1.field.widget.attrs.maxlength }}

Upvotes: 11

Related Questions