Python django.forms.forms 模块,NON_FIELD_ERRORS 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用django.forms.forms.NON_FIELD_ERRORS

项目:beg-django-e-commerce    作者:Apress    | 项目源码 | 文件源码
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
项目:fieldsight-kobocat    作者:awemulya    | 项目源码 | 文件源码
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})
项目:esdc-ce    作者:erigones    | 项目源码 | 文件源码
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))
项目:gougo    作者:amaozhao    | 项目源码 | 文件源码
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
项目:WorldsAtWar    作者:heidi666    | 项目源码 | 文件源码
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
项目:beg-django-e-commerce    作者:Apress    | 项目源码 | 文件源码
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
项目:esdc-ce    作者:erigones    | 项目源码 | 文件源码
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
项目:jiango    作者:yefei    | 项目源码 | 文件源码
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)