Is there any way to launch custom errors in the admin site like that?:
Currently I throw the error with
raise forms.ValidationError('error')
but shows the debug error screen
Where are you putting the raise forms.ValidationError('error')?
In your form clean() method is a good place to raise custom errors. You can even do def clean_fieldname() to preform specific validation for one field. From the django docs
from django import forms
class ContactForm(forms.Form):
# Everything as before.
...
def clean_recipients(self):
data = self.cleaned_data['recipients']
if "fred#example.com" not in data:
raise forms.ValidationError("You have forgotten about Fred!")
# Always return the cleaned data, whether you have changed it or
# not.
return data
This link also could help
Related
I am having difficulty raising a validation error using the Django Rest Framework.
I have an owner field. The owner field needs to access the request object. The documentation suggests using the following approach:
def pre_save(self, obj):
obj.owner = self.request.user
My problem is how to raise a validation error if this code fails. I tried raising an error inside the pre_save method, but this doesn't work properly. It actually results in an HTML response coming back from Django, rather than a message from the Django Rest Framework.
Use the django rest framework exceptions. For example:
from rest_framework.exceptions import ParseError
...
parsed_data = self.parse(some_data)
if not parsed_data:
raise ParseError('Some error occurred')
Also note that you won't see a 404 in there, that's because it uses the django.http.Http404 exception. These are returned by the API in a nice way.
Note:
If you are doing a significant amount of validation you might want to look at placing your logic in the serializer.
In my Django application, I have a forms.py file in which I define loads of classes corresponding to form input screens. Each of these classes does some validation either in that attribute/field specific _clean() functions or in the overall clean() function for the form class. If validation fails in any of these clean() functions, I raise an error like this:
raise forms.ValidationError('Invalid value dude!!')
When such an error is raised, the application catches the error and displays it on the form for the user.
I also have a logger defined at the top of this file like this:
import logging
logger = logging.getLogger(__name__)
Now, in addition to reporting to the user the ValidationErrors, I would like to catch them and log them as errors. Is there some generic and elegant way I can get the logger to log all such errors without changing any other behavior that affects the application's user?
You can extend your Forms to log the errors as soon as the validation ends:
class LoggingMixin(object):
def full_clean(self):
super(LoggingMixin, self).full_clean()
for field, errors in self.errors.iteritems():
logger.info('Form error in %s: %s', ', '.join(errors))
Then define your forms using the logging mixin:
class MyForm(LoggingMixin, forms.Form):
...
When Django 1.7 is out, you will also be able to catch the exceptions messages as they are raised. This will give you the raw exceptions as they have been raised (the Form does some more checking):
class LoggingMixin(object):
def add_error(self, field, error):
if field:
logger.info('Form error on field %s: %s', field, error)
else:
logger.info('Form error: %s', error)
super(LoggingMixin, self).add_error(field, error)
WARNING: Using add_error will do nothing in Django <= 1.6.
I am extending Django's AuthenticationForm in order to provide better error messaging to the user. If login is going to fail, I want to perform an additional check on the user's input to provide a less generic message explaining to the user why login failed.
urls:py:
urlpatterns = patterns(
'django.contrib.auth.views',
url(r'^login/$', 'login', {'authentication_form': RemoteLoginAwareLoginForm}, name='auth_login'),
)
forms.py:
class RemoteLoginAwareLoginForm(AuthenticationForm):
def clean(self):
cleaned_data = super(AuthenticationForm, self).clean()
if self.errors: # <==== self.errors is {}, even when login will fail
# Perform additional checks
return cleaned_data
I thought that the right way to do this was to override clean() as above, but as indicated, self.errors is empty even when I logged in with bad credentials. I tried to override full_clean() and validate() as well, but neither got called at all.
Update:
I realize that the error I'm looking for -- that the credentials aren't valid -- doesn't come until the code actually attempts to log the user in. I added a custom ModelBackend where I am able to catch the error... but I am not sure how to report it back up as there is no request here.
Is the answer that this requires a custom login view?
Thanks!
There is a similar problem on stackoverflow. But I'm not sure if it solves your problem.
Using AuthenticationForm in Django
I've some custom views in django-admin linked to my change_form.
All works well, but now I'd want to raise a ValidationError from my custom views and consequently get the flash in django-admin that prints the msg of ValidationError, that is the same that occurs if I raise it in model.clean().
an example of custom view that I use:
#site.admin_view
def send_transaction_mail(request, obj_id, typ):
order = Order.objects.get(id=obj_id)
if typ == 'SHIPMENT':
send_order_confirm(order)
else:
raise Exception("Something goes wrong sending transaction mail")
return HttpResponseRedirect(request.META['HTTP_REFERER'])
is there a way? Thank you
Not sure I understood what you want well:
You have a view, by definition a public page. You want it to display an error message in the admin pages (by definition privates page) ? It's odd. But if you want so.
To display an error in the admin pages, use the Django Message Framework. It's what is in use to display the yellow rows with errors/notifications on the top of the pages.
from django.contrib import messages
messages.error(request, "Something goes wrong sending transaction mail");
Indeed, validation errors an only displayed with forms. And thus, they are to be raised only in the clean() method of a form, a formset, or a field.
How do I add errors to the top of a form after I cleaned the data? I have an object that needs to make a REST call to an external app (google maps) as a pre-save condition, and this can fail, which means I need my users to correct the data in the form. So I clean the data and then try to save and add to the form errors if the save doesn't work:
if request.method == "POST":
#clean form data
try:
profile.save()
return HttpResponseRedirect(reverse("some_page", args=[some.args]))
except ValueError:
our_form.errors.__all__ = [u"error message goes here"]
return render_to_response(template_name, {"ourform": our_form,},
context_instance=RequestContext(request))
This failed to return the error text in my unit-tests (which were looking for it in {{form.non_field_errors}}), and then when I run it through the debugger, the errors had not been added to the forms error dict when they reach the render_to_response line, nor anywhere else in the our_form tree. Why didn't this work? How am I supposed to add errors to the top of a form after it's been cleaned?
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"])
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")
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.
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.
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.
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!"
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.