当前位置: 首页>>代码示例>>Python>>正文


Python ErrorDict.as_ul方法代码示例

本文整理汇总了Python中django.forms.util.ErrorDict.as_ul方法的典型用法代码示例。如果您正苦于以下问题:Python ErrorDict.as_ul方法的具体用法?Python ErrorDict.as_ul怎么用?Python ErrorDict.as_ul使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms.util.ErrorDict的用法示例。


在下文中一共展示了ErrorDict.as_ul方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: error

# 需要导入模块: from django.forms.util import ErrorDict [as 别名]
# 或者: from django.forms.util.ErrorDict import as_ul [as 别名]
 def error(self, request, status_code, error_dict=None):
     """
     Return JSON error response that includes a human readable error
     message, application-specific errors and a machine readable
     status code.
     """
     if not error_dict:
         error_dict = ErrorDict()
     response = HttpResponse(mimetype = self.mimetype)
     response.status_code = status_code
     response_dict = {
         "error-message" : '%d %s' % (status_code, STATUS_CODE_TEXT[status_code]),
         "status-code" : status_code,
         "model-errors" : error_dict.as_ul()
     }
     simplejson.dump(response_dict, response)
     return response
开发者ID:42cc,项目名称:django-rest-interface,代码行数:19,代码来源:responder.py

示例2: fork

# 需要导入模块: from django.forms.util import ErrorDict [as 别名]
# 或者: from django.forms.util.ErrorDict import as_ul [as 别名]
def fork(request, klass, id):
    """Create a new item of given klass.

    @param request: request data
    @type request: Django request
    @param klass: item's class for lookup in correct database table
    @type klass: either Data, Task or Method
    @return: user login page, item's view page or this page again on failed form validation
    @rtype: Django response
    @raise Http404: if given klass is unexpected
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('user_signin') + '?next=' + request.path)
    prev = klass.get_object(id)
    if not prev: raise Http404
    if not prev.can_fork(request.user):
        return HttpResponseForbidden()
    prev.klass = klass.__name__
    prev.name+=' (forked)'

    upload_limit = Preferences.objects.get(pk=1).max_data_size
    formfunc = eval(klass.__name__ + 'Form')
    if request.method == 'POST':
        form = formfunc(request.POST, request.FILES, request=request)

        # manual validation coz it's required for new, but not edited Data
        if not request.FILES and klass == Data:
            form.errors['file'] = ErrorDict({'': _('This field is required.')}).as_ul()

        # check whether file is too large
        if klass in (Data, Task) and 'file' in request.FILES:
            if len(request.FILES['file']) > upload_limit:
                form.errors['file'] = ErrorDict({'': _('File is too large!  Must be smaller than %dMB!' % (upload_limit / MEGABYTE))}).as_ul()

        if form.is_valid():
            new = form.save(commit=False)
            new.pub_date = datetime.datetime.now()
            try:
                new.slug = new.make_slug()
            except IntegrityError:
                # looks quirky...
                d = ErrorDict({'':
                    _('The given name yields an already existing slug. Please try another name.')})
                form.errors['name'] = d.as_ul()
            else:
                new.version = 1
                new.is_current = True
                new.is_public = False
                new.user = request.user

                if not form.cleaned_data['keep_private']:
                    new.is_public = True

                if klass == Data:
                    new.file = request.FILES['file']
                    new.num_instances = -1
                    new.num_attributes = -1
                    new.save()

                    # InMemoryUploadedFile returns file-like object whereas
                    # zipfile/tarfile modules used in get_uncompressed() require
                    # filename (prior to python 2.7), so we have to save it to
                    # disk, then rename, then save object again.
                    name_old = os.path.join(MEDIA_ROOT, new.file.name)
                    uncompressed = ml2h5.data.get_uncompressed(name_old)
                    if uncompressed:
                        os.remove(name_old)
                        name_old = uncompressed

                    new.format = ml2h5.fileformat.get(name_old)
                    name_new = os.path.join(DATAPATH, new.get_filename())
                    os.rename(name_old, os.path.join(MEDIA_ROOT, name_new))
                    new.file.name = name_new
                    new.save()
                elif klass == Task:
                    taskinfo = {
                        'train_idx': (form.cleaned_data['train_idx']),
                        'test_idx': (form.cleaned_data['test_idx']),
                        'input_variables': form.cleaned_data['input_variables'],
                        'output_variables': form.cleaned_data['output_variables']
                    }
                    new.file = None
                    if 'file' in request.FILES:
                        new.file = request.FILES['file']
                        new.save()
                        new.create_next_file(prev=None)
                    else:
                        new.save(taskinfo=taskinfo)
                    new.license = FixedLicense.objects.get(pk=1) # fixed to CC-BY-SA
                    new.save(taskinfo=taskinfo)
                elif klass == Method:
                    #if 'score' in request.FILES:
                    #    new.score = request.FILES['score']
                    #    new.score.name = new.get_scorename()
                    new.license = FixedLicense.objects.get(pk=1) # fixed to CC-BY-SA
                    new.save()
                elif klass == Challenge:
                    new.license = FixedLicense.objects.get(pk=1) # fixed to CC-BY-SA
                    new.save()
                    new.task=form.cleaned_data['task']
#.........这里部分代码省略.........
开发者ID:kidzik,项目名称:mldata,代码行数:103,代码来源:base.py

示例3: new

# 需要导入模块: from django.forms.util import ErrorDict [as 别名]
# 或者: from django.forms.util.ErrorDict import as_ul [as 别名]
def new(request, klass, default_arg=None):
    """Create a new item of given klass.

    @param request: request data
    @type request: Django request
    @param klass: item's class for lookup in correct database table
    @type klass: either Data, Task or Method
    @return: user login page, item's view page or this page again on failed form validation
    @rtype: Django response
    @raise Http404: if given klass is unexpected
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('user_signin') + '?next=' + request.path)

    upload_limit = Preferences.objects.get(pk=1).max_data_size
    formfunc = eval(klass.__name__ + 'Form')
    if request.method == 'POST':
        form = formfunc(request.POST, request.FILES, request=request)

        # manual validation coz it's required for new, but not edited Data
        if not request.FILES and klass == Data:
            form.errors['file'] = ErrorDict({'': _('This field is required.')}).as_ul()

        _validate_file_size(request, form, klass)
        
        if form.is_valid():
            new = form.save(commit=False)
            new.pub_date = datetime.datetime.now()
            try:
                new.slug = new.make_slug()
            except IntegrityError:
                # looks quirky...
                d = ErrorDict({'':
                    _('The given name yields an already existing slug. Please try another name.')})
                form.errors['name'] = d.as_ul()
            else:
                new.version = 1
                new.is_current = True
                new.is_public = False
                new.user = request.user

                if not form.cleaned_data['keep_private']:
                    new.is_public = True

                if klass == Data:
                    _upload_data_file(new, request.FILES['file'])
                    new.save()
                    form.save_m2m() # it couldn't be done automatically because of commit=False
                elif klass == Task:
                    new.license = FixedLicense.objects.get(pk=1) # fixed to CC-BY-SA
                    taskinfo = {
                        'train_idx': (form.cleaned_data['train_idx']),
                        'val_idx': (form.cleaned_data['val_idx']),
                        'test_idx': (form.cleaned_data['test_idx']),
                        'input_variables': form.cleaned_data['input_variables'],
                        'output_variables': form.cleaned_data['output_variables'],
                        'data_size': form.cleaned_data['data'].num_instances
                    }
                    new.file = None
                    if 'file' in request.FILES:
                        new.file = request.FILES['file']
                        new.save()
                        new.create_next_file(prev=None)
                    else:
                        new.save(taskinfo=taskinfo)
                elif klass == Method:
                    #if 'score' in request.FILES:
                    #    new.score = request.FILES['score']
                    #    new.score.name = new.get_scorename()
                    new.license = FixedLicense.objects.get(pk=1) # fixed to CC-BY-SA
                    new.save()
                elif klass == Challenge:
                    new.license = FixedLicense.objects.get(pk=1) # fixed to CC-BY-SA
                    new.save()
                    new.task=form.cleaned_data['task']
                    new.save()
                else:
                    raise Http404
                return HttpResponseRedirect(new.get_absolute_slugurl())
    else:
        if default_arg:
            form = formfunc(request=request, default_arg=default_arg)
        else:
            form = formfunc(request=request)
    kname=klass.__name__.lower()

    info_dict = {
        'klass': klass.__name__,
        kname: True,
        'uuid': uuid.uuid4(), # for upload progress bar
        'url_new': request.path,
        'form': form,
        'request': request,
        'tagcloud': get_tag_clouds(request),
        'section': 'repository',
        'upload_limit': "%dMB" % (upload_limit / MEGABYTE)
    }

    return _response_for(request, klass, 'item_new', info_dict)
开发者ID:kidzik,项目名称:mldata,代码行数:101,代码来源:base.py


注:本文中的django.forms.util.ErrorDict.as_ul方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。