Get type of Django form widget from within template

前端 未结 6 1516
你的背包
你的背包 2020-12-01 01:27

I\'m iterating through the fields of a form and for certain fields I want a slightly different layout, requiring altered HTML.

To do this accurately, I just need to

6条回答
  •  再見小時候
    2020-12-01 02:02

    Following up on the accepted answer - the enhanced if tag in Django 1.2 allows you to use filters in if tag comparisons. So you could now do your custom html/logic in the template like so:

      {% for field in form.fields %}
    • {% if field.field.widget|klass == "Textarea" %}

      Text Areas are Special

      {% else %} {{ field.errors }} {{ field.label_tag }} {{ field }} {% endif %}
    • {% endfor %}

提交回复
热议问题