Django - 如何让一个应用程序引用另一个应用程序

时间:2023-01-27 23:16:32

i have two Django apps in my Project, call them App1 and App2. App1 is the "Home"-application that displays a text to introduce users and shows some news. App2 is a "Content"-app to display pages that only consist of a title and some html retrieved from the DB. I want App1 to display it's text using the model from App2. I can do this by using

我的项目中有两个Django应用程序,称为App1和App2。 App1是“Home”应用程序,它显示文本以向用户介绍并显示一些新闻。 App2是一个“内容”-app,用于显示仅包含标题和从数据库中检索到的某些html的页面。我希望App1使用App2中的模型显示它的文本。我可以通过使用来做到这一点

from mysite.content.models import Content

and it works. But if I transfer my apps to another site, I would have to change that mysite to the name of the other site. Can this be done without supplying the name of the site?

它的工作原理。但是,如果我将我的应用程序转移到另一个站点,我将不得不将该mysite更改为其他站点的名称。这可以在不提供网站名称的情况下完成吗?

TIA :)

1 个解决方案

#1


mysite is just a directory name on the Python path, not a hostname.

mysite只是Python路径上的目录名,而不是主机名。

Give it a name that has meaning for your project and give the application directories names that have meaning for the applications.

给它一个对项目有意义的名称,并为应用程序提供对应用程序有意义的名称。

If you do this then you don't need to rename it if you move to another site. All you need to do is make sure your directory is on the Python path at the new site.

如果您这样做,那么如果您移动到另一个站点,则无需重命名。您需要做的就是确保您的目录位于新站点的Python路径上。

#1


mysite is just a directory name on the Python path, not a hostname.

mysite只是Python路径上的目录名,而不是主机名。

Give it a name that has meaning for your project and give the application directories names that have meaning for the applications.

给它一个对项目有意义的名称,并为应用程序提供对应用程序有意义的名称。

If you do this then you don't need to rename it if you move to another site. All you need to do is make sure your directory is on the Python path at the new site.

如果您这样做,那么如果您移动到另一个站点,则无需重命名。您需要做的就是确保您的目录位于新站点的Python路径上。