如何在 Django 中重用可重用应用程序

发布于 2024-07-13 22:55:31 字数 499 浏览 4 评论 0原文


我正在尝试在 Django 中创建我的第一个网站,当我寻找示例应用程序以从中汲取灵感时,我不断地偶然发现一个名为“可重用应用程序”的术语。

我理解可重复使用的应用程序的概念,但我对在 Django 中重复使用应用程序的方法完全不了解。 在整个行业中困扰我的问题很少有:

重用现有 Django 应用程序的首选方式是什么? 我应该把它放在哪里以及如何引用它?

据我了解,建议是将其放在“PYTHONPATH”上,但是一旦我需要将应用程序部署到我访问权限有限的远程位置(例如在托管服务上),这种情况就会中断。

因此,如果我在本地计算机上开发站点并打算将其部署在只有 ftp 访问权限的 ISP 上,我该如何重用第 3 方 Django 应用程序,以便在部署站点时该站点能够继续工作(例如我唯一可以信赖的是服务提供商安装了​​ Python 2.5 和 Django 1.x)?

如何组织我的 Django 项目,以便可以轻松地将其与我想要使用的所有可重用应用程序一起部署?


I am trying to create my first site in Django and as I'm looking for example apps out there to draw inspiration from, I constantly stumble upon a term called "reusable apps".

I understand the concept of an app that is reusable easy enough, but the means of reusing an app in Django are quite lost for me. Few questions that are bugging me in the whole business are:

What is the preferred way to re-use an existing Django app? Where do I put it and how do I reference it?

From what I understand, the recommendation is to put it on your "PYTHONPATH", but that breaks as soon as I need to deploy my app to a remote location that I have limited access to (e.g. on a hosting service).

So, if I develop my site on my local computer and intend to deploy it on an ISP where I only have ftp access, how do I re-use 3rd party Django apps so that if I deploy my site, the site keeps working (e.g. the only thing I can count on is that the service provider has Python 2.5 and Django 1.x installed)?

How do I organize my Django project so that I could easily deploy it along with all of the reusable apps I want to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-07-20 22:55:31

一般来说,使用可重用应用程序唯一需要做的就是确保它位于 sys.path 上,以便您可以从 Python 代码导入它。 在大多数情况下(如果作者遵循最佳实践),可重用的应用程序 tarball 或捆绑包将包含一个包含文档的顶级目录、一个 README、一个 setup.py,然后是一个包含实际文件的子目录。应用(请参阅 django-voting 示例;应用程序本身位于“投票”子目录中)。 该子目录需要放置在您的 Python 路径中。 可能的方法包括:

  • 如果应用程序已上传到 ,则运行 pip install appname PyPI(现在大多数都是)
  • 使用 setup.py install 安装应用程序(这与 pip install appname 具有相同的结果,但要求您首先下载并自行解压代码;pip 会为您执行此操作)
  • 手动将代码目录符号链接到您的 Python site-packages 目录
  • 使用 virtualenv 创建一个拥有自己的 site-packages 目录的“虚拟 Python 环境”,然后运行 ​​setup.py installpip install appname 激活 virtualenv,或者将应用程序放置或符号链接到 virtualenv 的站点包中(如果您重视未来的理智,强烈推荐所有“全局安装”选项)
  • 将应用程序放置在您打算放置各种应用程序的某个目录中,然后将该目录添加到 PYTHONPATH 环境变量中

如果您可以启动 Python 解释器并“导入投票”(例如)而不会出现 ImportError,那么您就会知道您已将其放置在正确的位置。

在您只能访问 FTP 的服务器上,您唯一的选择实际上是最后一个,并且他们必须为您进行设置。 如果他们声称支持 Django,他们必须提供一些可以上传包的地方,并且可以在 Python 中导入它们。 如果不了解您的网络主机的详细信息,就不可能说出他们如何为您构建它。

In general, the only thing required to use a reusable app is to make sure it's on sys.path, so that you can import it from Python code. In most cases (if the author follows best practice), the reusable app tarball or bundle will contain a top-level directory with docs, a README, a setup.py, and then a subdirectory containing the actual app (see django-voting for an example; the app itself is in the "voting" subdirectory). This subdirectory is what needs to be placed in your Python path. Possible methods for doing that include:

  • running pip install appname, if the app has been uploaded to PyPI (these days most are)
  • installing the app with setup.py install (this has the same result as pip install appname, but requires that you first download and unpack the code yourself; pip will do that for you)
  • manually symlinking the code directory to your Python site-packages directory
  • using software like virtualenv to create a "virtual Python environment" that has its own site-packages directory, and then running setup.py install or pip install appname with that virtualenv active, or placing or symlinking the app in the virtualenv's site-packages (highly recommended over all the "global installation" options, if you value your future sanity)
  • placing the application in some directory where you intend to place various apps, and then adding that directory to the PYTHONPATH environment variable

You'll know you've got it in the right place if you can fire up a Python interpreter and "import voting" (for example) without getting an ImportError.

On a server where you have FTP access only, your only option is really the last one, and they have to set it up for you. If they claim to support Django they must provide some place where you can upload packages and they will be available for importing in Python. Without knowing details of your webhost, it's impossible to say how they structure that for you.

妖妓 2024-07-20 22:55:31

这是一个老问题,但我是这么做的:

如果您使用版本控制系统 (VCS),我建议将您的软件所需的所有可重用应用程序和库(包括 django)放入 VCS 中。 如果您不想将它们直接放在项目根目录下,可以修改settings.py以将它们的位置添加到sys.path中。

之后,部署就像将 VCS 存储库克隆或签出到您想要使用的任何地方一样简单。

这有两个额外的好处:

  • 版本不匹配; 您的软件始终使用您测试时使用的版本,而不是部署时可用的版本。
  • 如果多人参与该项目,则没有其他人需要处理依赖项的安装。

当需要更新组件的版本时,请在 VCS 中更新它,然后通过它将更新传播到您的部署。

An old question, but here's what I do:

If you're using a version control system (VCS), I suggest putting all of the reusable apps and libraries (including django) that your software needs in the VCS. If you don't want to put them directly under your project root, you can modify settings.py to add their location to sys.path.

After that deployment is as simple as cloning or checking out the VCS repository to wherever you want to use it.

This has two added benefits:

  • Version mismatches; your software always uses the version that you tested it with, and not the version that was available at the time of deployment.
  • If multiple people work on the project, nobody else has to deal with installing the dependencies.

When it's time to update a component's version, update it in your VCS and then propagate the update to your deployments via it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文