我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.core.exceptions.NON_FIELD_ERRORS。
def clean(self): cleaned_data = super(AddToCartForm, self).clean() quantity = cleaned_data.get('quantity') if quantity is None: return cleaned_data try: product_variant = self.get_variant(cleaned_data) except ObjectDoesNotExist: msg = self.error_messages['variant-does-not-exists'] self.add_error(NON_FIELD_ERRORS, msg) else: cart_line = self.cart.get_line(product_variant) used_quantity = cart_line.quantity if cart_line else 0 new_quantity = quantity + used_quantity try: product_variant.check_quantity(new_quantity) except InsufficientStock as e: remaining = e.item.get_stock_quantity() - used_quantity if remaining: msg = self.error_messages['insufficient-stock'] self.add_error('quantity', msg % remaining) else: msg = self.error_messages['empty-stock'] self.add_error('quantity', msg) return cleaned_data
def non_field_errors(self): """ Returns an ErrorList of errors that aren't associated with a particular field -- i.e., from Form.clean(). Returns an empty ErrorList if there are none. """ return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))
def respond_error(request, form, message, code, template_name='news/formerror.html'): """ Return either a JSON or HTML error response @param request: the request @param form: the bound form object @param message: the error message @param code: the HTTP status code @param template_name: the template name in case of HTML response @return: HttpResponse object """ if request.is_ajax(): return HttpResponseJSON({ 'status': 'error', 'errors': [message], 'errors_by_field': {NON_FIELD_ERRORS: [message]} }, code) else: form.add_error(None, message) return render(request, template_name, {'form': form}, status=code)
def _update_errors(self, errors): for field, messages in errors.error_dict.items(): if field not in self.fields: continue field = self.fields[field] for message in messages: if isinstance(message, ValidationError): if message.code in field.error_messages: message.message = field.error_messages[message.code] message_dict = errors.message_dict for k, v in message_dict.items(): if k != NON_FIELD_ERRORS: self._errors.setdefault(k, self.error_class()).extend(v) # Remove the data from the cleaned_data dict since it was invalid if k in self.cleaned_data: del self.cleaned_data[k] if NON_FIELD_ERRORS in message_dict: messages = message_dict[NON_FIELD_ERRORS] self._errors.setdefault(NON_FIELD_ERRORS, self.error_class()).extend(messages)
def form_invalid(self, form): """ The user has provided invalid credentials (this was checked in AuthenticationForm.is_valid()). So now we set the test cookie again and re-render the form with errors. """ self.set_test_cookie() if form.has_error(NON_FIELD_ERRORS, 'admin_user'): return HttpResponseRedirect('/admin/') return super(LoginView, self).form_invalid(form)
def error_dict_full(self): """ ?????????? error_dict, ?? ????????????? ???????? ?????? ???? ?????. """ errors = self.error_dict if NON_FIELD_ERRORS in self.errors: errors = ({ 'name': NON_FIELD_ERRORS, 'fullname': NON_FIELD_ERRORS, 'class': '', 'errors': self.errors[NON_FIELD_ERRORS], }, ) + errors return errors
def addError(self, message): self._errors[NON_FIELD_ERRORS] = self.error_class([message])
def test_one_vollume_cant_have_multiple_root_chunks(self): new_chunk = VollumeChunk( vollume=self.vollume, author=create_and_save_dummy_user(username='Stevo'), text='top stuff' ) with self.assertRaises(ValidationError) as caught: new_chunk.full_clean() error_messages = caught.exception.message_dict[NON_FIELD_ERRORS] self.assertIn("A Vollume can't have more than one starting paragraph.", error_messages)
def test_author_of_first_chunk_must_also_be_vollume_author(self): vollume = Vollume( author=create_and_save_dummy_user(username='Mickey'), title='Top dollar mate' ) vollume.save() chunk = VollumeChunk( vollume=vollume, author=create_and_save_dummy_user(username='Steves'), text="Huh? Yeah, I just feel like... you know when you roll a blinding spliff?" ) with self.assertRaises(ValidationError) as caught: chunk.full_clean() error_messages = caught.exception.message_dict[NON_FIELD_ERRORS] self.assertIn("The author of the first paragraph of a Vollume must also be the Vollume author.", error_messages)
def add_error(self, field, error): """ Update the content of `self._errors`. The `field` argument is the name of the field to which the errors should be added. If its value is None the errors will be treated as NON_FIELD_ERRORS. The `error` argument can be a single error, a list of errors, or a dictionary that maps field names to lists of errors. What we define as an "error" can be either a simple string or an instance of ValidationError with its message attribute set and what we define as list or dictionary can be an actual `list` or `dict` or an instance of ValidationError with its `error_list` or `error_dict` attribute set. If `error` is a dictionary, the `field` argument *must* be None and errors will be added to the fields that correspond to the keys of the dictionary. """ if not isinstance(error, ValidationError): # Normalize to ValidationError and let its constructor # do the hard work of making sense of the input. error = ValidationError(error) if hasattr(error, 'error_dict'): if field is not None: raise TypeError( "The argument `field` must be `None` when the `error` " "argument contains errors for multiple fields." ) else: error = error.error_dict else: error = {field or NON_FIELD_ERRORS: error.error_list} for field, error_list in error.items(): if field not in self.errors: if field != NON_FIELD_ERRORS and field not in self.fields: raise ValueError( "'%s' has no field named '%s'." % (self.__class__.__name__, field)) if field == NON_FIELD_ERRORS: self._errors[field] = self.error_class(error_class='nonfield') else: self._errors[field] = self.error_class() self._errors[field].extend(error_list) if field in self.cleaned_data: del self.cleaned_data[field]