如何使用相同的数据设置不同站点的Django项目? [英] How do you setup a Django project with different sites using the same data?

查看:97
本文介绍了如何使用相同的数据设置不同站点的Django项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Django网站的文档:

I'm currently looking at the the documentation for Django sites:

http://docs.djangoproject.com/en/dev/ref/contrib/sites/#ref-contrib-sites

其中说明了如何将内容与多个网站相关联。所使用的示例是LJWorld.com和Lawrence.com。

which explains how to associate content with multiple sites. The example used is LJWorld.com and Lawrence.com.

上述Django项目结构如何?每个站点都是自己的应用程序,例如:

What does the Django project structure look like for the above? Is each site an app on its own, for instance:

project/
    manage.py
    settings.py
    urls.py
    ljworld/
        models.py
        views.py
    lawrence/
        models.py
        views.py

如果ljworld有 SITE_ID = 1 ,而劳伦斯 SITE_ID = 2 SITE_ID 变量必须在 ljworld / settings中显式设置。 py lawrence / settings.py

If ljworld has SITE_ID=1 and lawrence has SITE_ID=2, does the SITE_ID variable has to be explicitly set in ljworld/settings.py and lawrence/settings.py?

如何运行dev服务器不管是ljworld还是Lawrence?

How do you run the dev server of either ljworld or lawrence?

更新:

共享内容在上面。如果有不同的网站共享相同的内容,应该怎么做?我真的需要n个不同服务器上的n个不同的Django项目,所有这些都连接到同一个数据库服务器?

I used two sites with shared content in the above. What should be done if there are n different sites who are sharing the same content? Do I really need n different Django projects on n different servers, all connected to the same database server?

此外,如果我需要在settings.py中进行更改应该影响所有这些网站,手动更改这些文件将是非常繁琐的。

Moreover, if I need to make a change in settings.py which should affect all those web sites, it will be very tedious to change each of those files manually.

推荐答案

不,每个网站是不是自己的应用程序;每个网站都是自己的项目。整个想法是具有(完全或部分)共享内容的不同项目。所以你可能有一个结构,如:

No, each site is not an app on its own; each site is a project on its own. The whole idea is to have different projects with a (fully or partially) shared content. So you might have a structure such as:

ljworld/
    manage.py
    settings.py
    urls.py
    ljworld_specific_app1/
    ...
lawrence/
    manage.py
    settings.py
    urls.py
    lawrence_specific_app1/

您通常会使用两台Web服务器来投放项目 - 尽管通常都会参考相同的DB服务器。当然,您也可以在两个项目之间共享应用程序 - 只需将它们保留在服务器的PYTHONPATH中。

You would normally use two Web servers to serve the projects - though normally both would refer to the same DB server. Naturally you can also have apps which are shared between the two projects - just keep them somewhere in the server's PYTHONPATH.

编辑:

两个Web服务器当然不一定意味着两个不同的服务器。他们可能是在同一个Web服务器实例下运行的两个虚拟主机 - 你甚至可以将两个项目映射到同一虚拟主机下的两个不同的目录。

"Two Web servers" of course doesn't necessarily mean two physically different servers. They could well be two virtual hosts running under the same Web server instance - heck, you could even map the two projects to two different directories under the same virtual host.

共享设置,您可以使用与共享应用程序相同的技术。有一个global_settings模块,其中包含PYTHONPATH中某处的共享设置,并从每个settings.py导入。

For shared settings, you could use the same technique as for shared apps. Have a global_settings module which contains the shared settings available somewhere on the PYTHONPATH and import it from each of the settings.py.

如果你想要一些真正的黑客,你可以大概甚至可以删除所有不同的项目,只使用一个,并创建一个中间件,即时更改设置。但我会建议反对。

And if you wanted something really hackish, you could probably even drop all the different projects, use just one and create a middleware that changes settings on the fly. But I would advise against it.

这篇关于如何使用相同的数据设置不同站点的Django项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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