python-3.x - 从配置中添加渲染器到@view_config?

标签 python-3.x pyramid

如何向 @view_config 修饰的函数或类提供配置值?

例如

@view_config(route_name='example', renderer=some_config['template.name'])
class MyClass(BaseView):
    ...

或者

@view_defaults(route_name='example', renderer=some_config['template.name2'])
class MyClass2(BaseView):
    ...

或者

@view_config(route_name='example', renderer=some_config['template.name3'])
def method3(request):
    ...

很难知道从哪里开始,因为我正在尝试编辑一个 Pyramid 插件,该插件将其配置集中在 includeme 函数中,因此它没有任何明显的信息可以包含,并且很难知道 @view_config 装饰器可以使用什么。

最佳答案

您可以使用 declarative 添加 View 配置(您现在使用 @view_config 或通过调用 config.add_view() 方法使用 imperative configuration 执行的操作。

在这种情况下,由于您需要访问 Pyramid 注册表和设置文件,因此命令式添加 View 会更容易。

在您的__init__.py中您可以执行以下操作:

  settings = config.registry.settings
  # You need to call config.add_route("foobar") to map view to URL also

  config.add_view('views.MyClass', route_name="foobar", renderer=settings['template.name3'])

然后在你的views.py中:

  class MyClass(BaseView):
       pass    

@view_config()add_view()参数是相等的。

我认为您还可以为同一 View 混合使用 view_configadd_view() 参数,但我不确定这一点。希望这会有所帮助。

关于python-3.x - 从配置中添加渲染器到@view_config?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804382/

相关文章:

python如何退出屏保

python - 尝试将列表添加到列表时出现 TypeError

python - 在 Elastic Beanstalk 上部署 Pyramid 应用程序

python - 如何访问 Mako 模板和 Pyramid 中的 session 变量?

python - Pyramid 中的 virtualenv + setuptools 问题

python - 是否有适用于 Python 3.7.1 的 Microsoft Visual C++ 编译器?

python 3.6 : "AttributeError: ' SimpleQueue' object has no attribute '_poll' "

linux - 无法将 C++ 扩展上传到 Colab

sqlite - 击中 sqlalchemy.exc.IntegrityError : (IntegrityError) constraint failed 'INSERT INTO users

python - 如何使用 pyramid_sockjs 的消息传递?