18👍
You really want to do this during form validation and raise a ValidationError from there… but if you’re set on doing it this way you’ll want to access _errors
to add new messages. Try something like this:
from django.forms.util import ErrorList
our_form._errors["field_name"] = ErrorList([u"error message goes here"])
12👍
Non field errors can be added using the constant NON_FIELD_ERRORS
dictionary key (which is __all__
by default):
from django import forms
errors = my_form._errors.setdefault(forms.forms.NON_FIELD_ERRORS, forms.util.ErrorList())
errors.append("My error here")
- SAML with Django authentication
- Django: Generic views based 'as_view()' method
- Dynamically loading django apps at runtime
- Passing list of values to django view via jQuery ajax call
12👍
In Django 1.7 or higher, I would do:
form.add_error(field_name, "Some message")
The method add_error
was added in 1.7. The form
variable is the form I want to manipulate and field_name
is the specific field name or None
if I want an error that is not associated with a specific field.
In Django 1.6 I would do something like:
from django.forms.forms import NON_FIELD_ERRORS
errors = form._errors.setdefault(field_name, form.error_class())
errors.append("Some message")
In the code above form
is the form I want to manipulate and field_name
is the field name for which I want to add an error. field_name
can be set to NON_FIELD_ERRORS
to add an error not associated with a specific field. I use form.error_class()
to generate the empty list of error messages. This is how Django 1.6 internally creates an empty list rather than instantiate ErrorList()
directly.
- Show images in Django templates
- Django+Nginx+uWSGI = 504 Gateway Time-out
- Should I use Celery or Carrot for a Django project?
9👍
You should raise the validationerror.
Why not put the verification within the form’s clean method
class ProfileForm(forms.Form):
def clean(self):
try:
#Make a call to the API and verify it works well
except:
raise forms.ValidationError('Your address is not locatable by Google Maps')
that way, you just need the standard form.is_valid()
in the view.
- Whats the correct format for django dateTime?
- Customizing django admin ChangeForm template / adding custom content
8👍
You’re almost there with your original solution. Here is a base Form class I built which allows me to do the same thing, i.e. add non-field error messages to the form:
from django import forms
from django.forms.util import ErrorDict
from django.forms.forms import NON_FIELD_ERRORS
class MyBaseForm(forms.Form):
def add_form_error(self, message):
if not self._errors:
self._errors = ErrorDict()
if not NON_FIELD_ERRORS in self._errors:
self._errors[NON_FIELD_ERRORS] = self.error_class()
self._errors[NON_FIELD_ERRORS].append(message)
class MyForm(MyBaseForm):
....
All my forms extend this class and so I can simply call the add_form_error() method to add another error message.
- Django+Nginx+uWSGI = 504 Gateway Time-out
- Django – Custom Admin Actions Logging
- How to display month name by number?
- Can I detect if my code is running on cPython or Jython?
2👍
I’m not sure how horrible of a hack this is (I’ve only really worked on two Django projects up until this point) but if you do something like follows you get a separate error message that is not associated with a specific field in the model:
form = NewPostForm()
if something_went_horribly_wrong():
form.errors[''] = "You broke it!"
0👍
If the validation pertains to the data layer, then you should indeed not use form validation. Since Django 1.2 though, there exists a similar concept for Django models and this is certainly what you shoud use. See the documentation for model validation.