Question

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!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top