扩展我的应用程序 - Pyramid/Pylons/Python
关于扩展我的应用程序的简单问题
假设我有一个“主应用程序”,在这个应用程序中,我在 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必复制代码即可执行此操作。使用 Configurator.include 方法将“主应用程序”配置包含在您的新应用程序。该文档很好地解释了这一点 这里和这里,但本质上,如果你在可调用文件中声明您的主应用程序配置:
然后您可以将主应用程序包含在新应用程序的配置中,如下所示:
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:
Then you can include your main app in your new app's configuration like this: