我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用django.forms.forms.NON_FIELD_ERRORS。
def include_form_fields(form=None, fields=None, as_choice='as_table', global_errors=True): fields=fields.replace(' ', '').split(',') if not global_errors: form.errors[NON_FIELD_ERRORS] = form.error_class() fields_backup = deepcopy(form.fields) form.fields = SortedDict() for field in fields: if field in fields_backup: form.fields[field] = fields_backup[field] resulting_text = getattr(form, as_choice)() form.fields = fields_backup return resulting_text
def post(self, request, pk=id): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): try: sitefile=request.FILES['file'] user = request.user print sitefile task = bulkuploadsites.delay(user, sitefile, pk) if CeleryTaskProgress.objects.create(task_id=task.id, user=user, task_type=0): messages.success(request, 'Sites are being uploaded. You will be notified in notifications list as well.') else: messages.success(request, 'Sites cannot be updated a the moment.') return HttpResponseRedirect(reverse('fieldsight:proj-site-list', kwargs={'pk': pk})) except Exception as e: form.full_clean() form._errors[NON_FIELD_ERRORS] = form.error_class(['Sites Upload Failed, UnSupported Data', e]) messages.warning(request, 'Site Upload Failed, UnSupported Data ') return render(request, 'fieldsight/upload_sites.html', {'form': form, 'project': pk})
def set_api_errors(self, data): """All errors except content related are transformed to non_field_errors""" errors = data.get('result', data) if isinstance(errors, dict) and errors: if NON_FIELD_ERRORS not in self._errors: self._errors[NON_FIELD_ERRORS] = self.error_class() for key, val in errors.items(): if key in self.fields: self._errors[key] = self.error_class(val) else: if not isinstance(val, list): val = (val,) for err in val: self._errors[NON_FIELD_ERRORS].append('%s: %s' % (key, err))
def save(self): """ Attempts to move the node using the selected target and position. If an invalid move is attempted, the related error message will be added to the form's non-field errors and the error will be re-raised. Callers should attempt to catch ``InvalidNode`` to redisplay the form with the error, should it occur. """ try: self.node.move_to(self.cleaned_data['target'], self.cleaned_data['position']) return self.node except InvalidMove as e: self.errors[NON_FIELD_ERRORS] = self.error_class(e) raise
def nice_errors(form, non_field_msg='General form errors'): nice_errors = ErrorDict() if isinstance(form, forms.BaseForm): for field, errors in form.errors.items(): if field == NON_FIELD_ERRORS: key = non_field_msg else: key = form.fields[field].label nice_errors[key] = errors return nice_errors
def exclude_form_fields(form=None, fields=None, as_choice='as_table', global_errors=True): fields=fields.replace(' ', '').split(',') if not global_errors: form.errors[NON_FIELD_ERRORS] = form.error_class() fields_backup = deepcopy(form.fields) for field in fields: if field in form.fields: del form.fields[field] resulting_text = getattr(form, as_choice)() form.fields = fields_backup return resulting_text
def _set_api_errors(self, data): # errors is a dict error output from API if not data or not isinstance(data, dict): return None errors = data.get('result', data) if isinstance(errors, dict): # Classic serializer error task output # Pair API errors to Django form errors for field in self.fields: if field in errors: self._add_error(field, errors[field]) # should be lazy try: del self.cleaned_data[field] except KeyError: pass if 'non_field_errors' in errors: self._add_error(NON_FIELD_ERRORS, errors['non_field_errors']) # should be lazy elif 'detail' in errors: self._add_error(NON_FIELD_ERRORS, ugettext(errors['detail'])) # should be noop else: # More serious api error if isinstance(errors, list): # Maybe we have errors from multiple serializers for err in errors: self._set_api_errors(err) elif errors: self._add_error(NON_FIELD_ERRORS, errors) self._set_custom_api_errors(errors) # noinspection PyUnusedLocal
def __init__(self, form): errors = [] if not form.is_valid(): for f, e in form.errors.items(): label = None if f != NON_FIELD_ERRORS: label = form[f].label errors.append({'field': f, 'message': e, 'label': label}) super(FormError, self).__init__(errors)