11

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

3 Answers 3

16

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 }}
1
  • That is a great answer, but I would like to extend it. If you are looping through the form fields and you need to add additional tags (because you are using bootstrap like me), you will need something like: {% for field in form %} {% if field.field.widget.attrs.class %} in order to determine if you need to add stuff
    – NFSpeedy
    Dec 2, 2022 at 10:08
11

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 }}
1
  • Thanks for suggesting he go the .errors route but still answering the question. Mar 29, 2013 at 15:19
2

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 %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.