Skip to content

Instantly share code, notes, and snippets.

@rdandy
Created June 28, 2012 08:41
Show Gist options
  • Save rdandy/3009951 to your computer and use it in GitHub Desktop.
Save rdandy/3009951 to your computer and use it in GitHub Desktop.
use this __init__() for display custom error messages in django forms.py
class MyForm(forms.Form):
username = forms.CharField(label=_('Your username'), min_length=3, max_length=30)
email = forms.EmailField(label=_('Your email address'), min_length=3, max_length=30)
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
field.error_messages = {
'required': ugettext('The field : {fieldname} is required !!').format(fieldname=field.label),
'invalid': ugettext('The field : {fieldname} is invalid !!').format(fieldname=field.label)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment