开发者

Extending my application - Pyramid/Pylons/Python

开发者 https://www.devze.com 2023-04-04 03:59 出处:网络
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:

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!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消