Add Class to All Error Fields in Form


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def __init__(self, *args, **kwargs):
  2. super(form, self).__init__(*args, **kwargs)
  3. for field in self.errors:
  4. if not field == '__all__':
  5. # errors dict can have key __all__ for non field errors.
  6. self.fields[field].widget.attrs['class'] = \
  7. self.fields[field].widget.attrs.get('class', '') + 'error'

URL: http://stackoverflow.com/questions/5339288/rendering-required-and-error-classes-with-django-form-fields-in-a-custom-templat

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.