[Fixed]-How to add attributes to form field in template

1👍

doing this via a filter was the easiest option.

from django import template
register = template.Library()

@register.filter(name='addcss')
def addcss(field, css):
   return field.as_widget(attrs={"class":css})

in template:

{{field|addcss:"form-control"}}

source: http://vanderwijk.info/blog/adding-css-classes-formfields-in-django-templates/

👤Yunti

Leave a comment