打包 django 应用程序的好方法是什么?
我有一个 django 项目,由客户在他们的服务器上安装。我还有一些应用程序,它们是可以安装/卸载的可选功能插件。
我想要一种简单的方法来打包这些插件应用程序,以使安装/卸载变得轻松。我不希望他们将模板文件复制到一个目录,将应用程序复制到另一个目录,将媒体复制到第三个目录,依此类推。我希望他们不需要编辑settings.py,尽管如果没有帮助也没关系。
理想的情况是,他们可以简单地解压缩到 python 路径上的某个位置(也许是一个特殊的插件目录?),然后将其删除以进行卸载。有没有一种简单的方法来打包应用程序以便可以通过这种方式安装它们?
I have a django project which is installed by customers on their servers. I've got a few more apps which are optional plugins of functionality that can be installed/uninstalled.
I'd like a simple way to package these plugin apps to make the install/uninstall painless. I dont want them to copy the template files to one directory, app to another one, media to a third one and so on. I would prefer that they need not edit settings.py, though its okay if it can't be helped.
The ideal situation would be if they could simply unzip to a location on the python path (maybe a special plugin directory?), and delete it to uninstall. Is there an easy way to package the apps so that they can be installed this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将跳过对 Python 打包(distutils、setuptools、pip 等)的讨论,因为听起来您更喜欢使用简单的 zip 文件或 tarball。我将一次解决您提到的“痛点”:
模板文件:只要您项目的 TEMPLATE_LOADERS 设置中包含“django.template.loaders.app_directories.load_template_source” ,您不必担心这一点。您的每个应用程序都可以有一个“templates/”子目录,其中的模板将被加载,就像它们位于项目范围的模板目录中一样。
媒体文件:应用媒体是一种痛苦。对于开发,您可以使用自定义的serve_media视图,其操作类似于app_directories模板加载器(在每个应用程序中查找媒体)。在生产中,您必须复制文件、使用符号链接或使用网络服务器级别名。有几个实用程序应用程序试图解决这个问题;我现在使用 django-staticfiles。
编辑settings.py:没有简单的方法可以解决这个问题。为了使其模型、模板标签、管理命令等正常工作,应用程序必须列在 INSTALLED_APPS 中。您可以做的是在 settings.py 中编写一些自定义代码,列出某个目录的内容,并将在那里找到的包动态添加到 INSTALLED_APPS 中。有点危险(仔细考虑谁有权将文件放入该目录,因为他们拥有您王国的钥匙),并且只有在服务器重新加载时才会检测到那里的新文件,但它应该可以工作。
我认为如果你把这些解决方案放在一起,就有可能实现你理想的情况:解压安装,删除卸载。
I'll skip over discussion of Python packaging (distutils, setuptools, pip, etc), since it sounds like you'd prefer using simple zip files or tarballs. I'll address the "pain points" you mentioned one at a time:
Template files: As long as you have 'django.template.loaders.app_directories.load_template_source' included in the TEMPLATE_LOADERS setting of your projects, you shouldn't have to worry about this one. Each of your apps can have a "templates/" subdirectory, and templates in there will be loaded just as if they were in your project-wide templates directory.
Media files: App media is a pain. For development, you can use a custom serve_media view that operates similarly to the app_directories template loader (looks for media in each app). In production, you have to either copy the files, use symbolic links, or use webserver-level aliases. There are several utility apps out there that try to smooth over this problem; I now use django-staticfiles.
Editing settings.py: No simple way around this one. For its models, template tags, management commands, etc to work, an app has to be listed in INSTALLED_APPS. What you could do is write some custom code in your settings.py that lists the contents of a certain directory and dynamically adds the packages it finds there to INSTALLED_APPS. A little bit dangerous (think carefully about who has permissions to place files in that directory, because they have the keys to your kingdom), and new files there will only be detected on a server reload, but it should work.
I think if you put together those solutions, it's possible to achieve your ideal situation: unzip to install, delete to uninstall.
编辑settings.py:您的插件可以从其自己目录中的设置文件中读取其设置。他们只需要编辑根 settings.py 即可在“INSTALLED_APPS”中添加/删除插件路径。
Editing settings.py: Your plugin can read its settings from its own settings file in its own directory. They'd only need to edit the root settings.py to add/remove the plug-in path from "INSTALLED_APPS".