Get type of Django form widget from within template

前端 未结 6 1519
你的背包
你的背包 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 01:50

    Following the answer from Oli and rinti: I used this one and I think it is a bit simpler:

    template code: {{ field|fieldtype }}

    filter code:

    from django import template
    register = template.Library()
    
    @register.filter('fieldtype')
    def fieldtype(field):
        return field.field.widget.__class__.__name__
    

提交回复
热议问题