【问题标题】:upgrading existing config.add_route, pyramid 1.4 to 1.10将现有的 config.add_route,金字塔 1.4 升级到 1.10
【发布时间】:2021-06-03 23:00:51
【问题描述】:

我正在处理的代码:

def add(self, path, view, method='GET'):
        url = _join_url_paths(self.path_prefix, path)
        id_ = hashlib.md5('%s-%s' % (url, method)).hexdigest()
        view = _join_view_paths(self.view_prefix, view)
        if id_ in _routes and env.name == 'dev':
            mlog.debug('Detected duplicate route: %s, %s' % (url, view))
        _routes[id_] = Route(id_, url, view, method, self.model, self.parent)
        if self.model and path == '/' and self.model not in _model_routes:
            _model_routes[self.model] = _routes[id_]
        self.config.add_route(id_, url, view=view, request_method=method)

这在金字塔 1.4 上完美运行,到目前为止,我已经查看了 1.4 源代码和 1.10 源代码,这是我发现的差异但我似乎找不到修复它的方法。

参考 1.4

class RoutesConfiguratorMixin(object):
    @action_method
    def add_route(self,
                  name,
                  pattern=None,
                  view=None,
                  view_for=None,
                  permission=None,
                  factory=None,
                  for_=None,
                  header=None,
                  xhr=None,
                  accept=None,
                  path_info=None,
                  request_method=None,
                  request_param=None,
                  traverse=None,
                  custom_predicates=(),
                  view_permission=None,
                  renderer=None,
                  view_renderer=None,
                  view_context=None,
                  view_attr=None,
                  use_global_views=False,
                  path=None,
                  pregenerator=None,
                  static=False,
                  **predicates):

参考 1.10

class RoutesConfiguratorMixin(object):
    @action_method
    def add_route(
        self,
        name,
        pattern=None,
        factory=None,
        for_=None,
        header=None,
        xhr=None,
        accept=None,
        path_info=None,
        request_method=None,
        request_param=None,
        traverse=None,
        custom_predicates=(),
        use_global_views=False,
        path=None,
        pregenerator=None,
        static=False,
        **predicates
    ):

请注意,金字塔 1.10 版本的 add_route 中没有参数 view,有什么建议吗?我正在查看更改日志,如果我碰巧早点找到它,这可能是一个有趣的解决方案,可以发布在平台上。

【问题讨论】:

    标签: python-2.7 view upgrade pyramid


    【解决方案1】:

    从更改说明到1.5a2 (2013-09-22), Backwards Incompatibilities

    删除了将以下参数传递给 pyramid.config.Configurator.add_route 的功能:viewview_contextview_forview_permissionview_rendererview_attr。自 Pyramid 1.1 以来,使用这些参数已被弃用。不要将与视图相关的参数传递给add_route,而是使用对pyramid.config.Configurator.add_view 的单独调用来使用其route_name 参数将视图与路由相关联。请注意,这也会影响pyramid.config.Configurator.add_static_view 函数,因为它委托给add_route

    例如:

    self.config.add_route(id_, url)
    self.config.add_view(view=my_view_callable, route_name=id_, request_method=method)
    

    有关详细信息,请参阅View Configuration 上的文档。我更喜欢使用函数装饰器进行视图配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多