什么是打包django应用程序的好方法? [英] What is a good way to package django apps?

查看:132
本文介绍了什么是打包django应用程序的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django项目,由客户在他们的服务器上安装。我还有更多的应用程序是可以安装/卸载的功能的可选插件。



我想要一个简单的方法来打包这些插件应用程序,使安装/卸载变得无痛。我不希望他们将模板文件复制到一个目录,应用程序到另一个目录,媒体到第三个等等。我更喜欢他们不需要编辑settings.py,但如果不能帮助它,它还可以。



理想的情况是,如果他们可以简单地解压缩到python路径上的某个位置(也许是一个特殊的插件目录),并将其删除以卸载。有没有一个简单的方法来打包应用程序,以便可以这样安装?

解决方案

我将跳过讨论Python包装(distutils,setuptools,pip等),因为它听起来像您喜欢使用简单的zip文件或tarballs。我会解决你一次提到的痛点:



模板文件:只要你有'django.template .loaders.app_directories.load_template_source包含在你的项目的TEMPLATE_LOADERS设置中,你不用担心这个。您的每个应用程序都可以有一个templates /子目录,其中的模板将被加载,就像它们在项目范围的模板目录中一样。



媒体档案:应用程式媒体是一种痛苦。对于开发,您可以使用与app_directories模板加载器类似的自定义serve_media视图(在每个应用程序中查找媒体)。在生产中,您必须复制文件,使用符号链接或使用网络服务器级别的别名。有几个实用程序应用程序试图平滑这个问题;我现在使用 django-staticfiles



编辑settings.py :没有简单的方法围绕这一个。为了使其模型,模板标签,管理命令等工作,应用程序必须在INSTALLED_APPS中列出。您可以做的是在settings.py中编写一些自定义代码,其中列出了某个目录的内容,并将其发现的包动态添加到INSTALLED_APPS。有点危险(仔细想想有谁拥有将文件放置在该目录中的权限,因为它们具有您的王国的密钥),并且只有在服务器重新加载时才会检测到新文件,但它应该可以工作。



我想如果你把这些解决方案放在一起,可以实现你的理想情况:解压安装,删除卸载。


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?

解决方案

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.

这篇关于什么是打包django应用程序的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆