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
python类NON_FIELD_ERRORS的实例源码
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 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 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 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 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 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 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 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 addError(self, message):
self._errors[NON_FIELD_ERRORS] = self.error_class([message])
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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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'))