扩展我的应用程序 - Pyramid/Pylons/Python

发布于 2024-12-03 19:29:07 字数 709 浏览 2 评论 0原文

关于扩展我的应用程序的简单问题

假设我有一个“主应用程序”,在这个应用程序中,我在 _init_.py 文件中具有以下内容:

config.add_route('image_upload', '/admin/image_upload/', 
    view='mainapp.views.uploader',
    view_renderer='/site/upload.mako')

在views.py 中我有:

def uploader(request):
    # some code goes here
    return {'xyz':xyz}

现在,当我创建一个新应用程序时,我想要扩展它,使用上面的视图和路由:

在新的应用程序 _init_.py 文件中,我将手动复制 config.add_route 代码:

config.add_route( 'image_upload', '/admin/image_upload/', 
   view='mainapp.views.uploader', 
   view_renderer='mainapp:templates/site/upload.mako'
 )

这就是我需要做的所有事情吗?由此,我的应用程序能够使用主应用程序中的视图和模板,还是我缺少其他内容?

感谢您的阅读!

Simple question about extending my application

Lets say I have a "Main Application", and in this application I have the following in the _init_.py file:

config.add_route('image_upload', '/admin/image_upload/', 
    view='mainapp.views.uploader',
    view_renderer='/site/upload.mako')

and in the views.py I have:

def uploader(request):
    # some code goes here
    return {'xyz':xyz}

Now when I create a new application, and I want to extend it, to use the above view and route:

In the new application _init_.py file I would manually copy over the config.add_route code:

config.add_route( 'image_upload', '/admin/image_upload/', 
   view='mainapp.views.uploader', 
   view_renderer='mainapp:templates/site/upload.mako'
 )

And is that all I would need to do? From this would my application be able to use the view and template from the main application, or is am I missing something else?

Thanks for reading!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梓梦 2024-12-10 19:29:07

您不必复制代码即可执行此操作。使用 Configurator.include 方法将“主应用程序”配置包含在您的新应用程序。该文档很好地解释了这一点 这里这里,但本质上,如果你在可调用文件中声明您的主应用程序配置:

def main_app_config(config):

    config.add_route('image_upload', '/admin/image_upload/', 
    view='mainapp.views.uploader',
    view_renderer='/site/upload.mako')

然后您可以将主应用程序包含在新应用程序的配置中,如下所示:

from my.main.app import main_app_config

# do your new application Configurator setup, etc.
# then "include" it.

config.include(main_app_config)

# continue on with your new app configuration

You don't have to copy your code to do this. Use the Configurator.include method to include your "Main Application" configuration in your new application. The documentation explains this pretty well both here and here, but the essentially, if you declare your main apps configuration inside a callable:

def main_app_config(config):

    config.add_route('image_upload', '/admin/image_upload/', 
    view='mainapp.views.uploader',
    view_renderer='/site/upload.mako')

Then you can include your main app in your new app's configuration like this:

from my.main.app import main_app_config

# do your new application Configurator setup, etc.
# then "include" it.

config.include(main_app_config)

# continue on with your new app configuration
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文