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


Python Response.errors方法代码示例

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


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

示例1: form_reprint

# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import errors [as 别名]
def form_reprint(
    request,
    form_print_method,
    form_stash=DEFAULT_FORM_STASH,
    render_view=None,
    render_view_template=None,
    auto_error_formatter=formatter_nobr,
    error_formatters=None,
    **htmlfill_kwargs
):
    """reprint a form
        args:
        ``request`` -- request instance
        ``form_print_method`` -- bound method to execute
        kwargs:
        ``frorm_stash`` (pyramid_formencode_classic.DEFAULT_FORM_STASH = _default) -- specify a stash
        ``auto_error_formatter`` (formatter_nobr) -- specify a formatter for rendering errors
            this is an htmlfill_kwargs, but we default to one without a br
        ``error_formatters`` (default None) is a dict of error formatters to be passed into htmlfill.
            in order to ensure compatibilty, this dict will be merged with a copy of the htmlfill defaults,
            allowing you to override them or add extras.
        `**htmlfill_kwargs` -- passed on to htmlfill
    """
    if __debug__:
        log.debug("form_reprint - starting...")

    response = None
    if form_print_method:
        response = form_print_method()
    elif render_view and render_view_template:
        response = PyramidResponse(pyramid_render(render_view_template, render_view(), request=request))
    else:
        raise ValueError("invalid args submitted")

    # If the form_content is an exception response, return it
    # potential ways to check:
    # # hasattr(response, 'exception')
    # # resposne.code != 200
    # # repsonse.code == 302 <-- http found
    if hasattr(response, 'exception'):
        if __debug__:
            log.debug("form_reprint - response has exception, redirecting")
        return response

    formStash = request.pyramid_formencode_classic[form_stash]
    
    if __debug__:
        _debug = {'print_method': str(form_print_method),
                  'render_view': str(render_view),
                  'render_view_template': str(render_view_template),
                  'auto_error_formatter': str(auto_error_formatter),
                  'error_formatters': str(error_formatters),
                  }
        formStash._reprints.append(_debug)

    form_content = response.text

    # Ensure htmlfill can safely combine the form_content, params and
    # errors variables (that they're all of the same string type)
    if not formStash.is_unicode_params:
        if __debug__:
            log.debug("Raw string form params: ensuring the '%s' form and FormEncode errors are converted to raw strings for htmlfill", form_print_method)
        encoding = determine_response_charset(response)

        if hasattr(response, 'errors'):
            # WSGIResponse's content may (unlikely) be unicode in Python2
            if six.PY2:
                if isinstance(form_content, six.text_type):  # PY2=unicode()
                    form_content = form_content.encode(encoding, response.errors)

            # FormEncode>=0.7 errors are unicode (due to being localized via ugettext). Convert any of the possible formencode unpack_errors formats to contain raw strings
            response.errors = encode_formencode_errors({}, encoding, response.errors)

    elif not isinstance(form_content, six.text_type):  # PY2=unicode()
        if __debug__:
            log.debug("Unicode form params: ensuring the '%s' form is converted to unicode for htmlfill", formStash)
        # py3 - test
        encoding = determine_response_charset(response)
        form_content = form_content.decode(encoding)

    # copy these because we don't want to overwrite a dict in place
    _htmlfill_kwargs = htmlfill_kwargs.copy()
    _htmlfill_kwargs.setdefault('encoding', request.charset)
    if error_formatters is not None:
        _error_formatters = dict(list(formencode.htmlfill.default_formatter_dict.items()) + list(error_formatters.items()))
        _htmlfill_kwargs['error_formatters'] = _error_formatters

    # _form_content = form_content
    form_content = formencode.htmlfill.render(
        form_content,
        defaults = formStash.defaults,
        errors = formStash.errors,
        auto_error_formatter = auto_error_formatter,
        **_htmlfill_kwargs
    )
    # import pdb
    # pdb.set_trace()
    response.text = form_content
    return response
开发者ID:jvanasco,项目名称:pyramid_formencode_classic,代码行数:101,代码来源:__init__.py


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