Skip to content

Instantly share code, notes, and snippets.

@eliasson
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasson/ce120a9c9fa4f679575b to your computer and use it in GitHub Desktop.
Save eliasson/ce120a9c9fa4f679575b to your computer and use it in GitHub Desktop.
Add CSS class for form widget in Django Templates
from django import template
register = template.Library()
@register.filter(name='addcss')
def addcss(field, css):
"""
Adds the given class to the class attribute of the given field.
E.g. adds the class 'bar' to the foo widget:
{{ form.foo|addcss:"bar" }}
"""
return field.as_widget(attrs={"class": css})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment