具有共享代码库和数据库的多个 Django 站点 [英] Multiple Django sites with shared codebase and DB

查看:23
本文介绍了具有共享代码库和数据库的多个 Django 站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 20 个不同的国家/地区创建了一个 Django 项目,其中包含 20 个站点(每个站点一个不同的域).这些站点共享所有内容:代码库、数据库、网址、模板等.

I have created a Django proyect with 20 sites (one different domain per site) for 20 different countries. The sites share everything: codebase, database, urls, templates, etc.

他们唯一不共享的是我在每个站点设置文件中设置的小的自定义(徽标、CSS 主题的背景颜色、语言代码等)(每个站点都有一个设置文件,并且所有这些文件中的一个导入具有通用内容的全局设置文件).现在,为了在开发模式下运行站点,我将执行以下操作:

The only thing they don't share are small customizations (logo, background color of the CSS theme, language code, etc.) that I set in each of the site settings file (each site has one settings file, and all of these files import a global settings file with the common stuff). Right now, in order to run the sites in development mode I'll do:

django-admin.py runserver 8000 --settings=config.site_settings.site1
django-admin.py runserver 8001 --settings=config.site_settings.site2
...
django-admin.py runserver 8020 --settings=config.site_settings.site20

我有几个问题:

  1. 我了解到可以为每个站点(域)创建一个虚拟主机并将其传递给站点的 settings.py 文件.但是,我担心这会为每个站点创建一个 Django 实例.我说得对吗?
  2. 是否有更有效的部署方式?我读过关于 django-dynamicsites 但我不确定如果这是正确的方式.
  3. 如果我决定使用 Heroku 进行部署,Heroku 似乎希望每个应用程序只有一个设置文件,因此我需要有 20 个应用程序.有解决办法吗?
  1. I've read that it is possible to create a virtual host for each site (domain) and pass it the site's settings.py file. However, I am afraid this would create one Django instance per site. Am I right?
  2. Is there a more efficient way of doing the deployment? I've read about django-dynamicsites but I am not sure if it is the right way to go.
  3. If I decide to deploy using Heroku, it seems that Heroku expects only one settings file per app, so I would need to have 20 apps. Is there a solution for that?

谢谢!

推荐答案

所以,我最近做了类似的事情,发现下面的策略是最好的选择.我将假设您此时熟悉 git 分支以及 Heroku 遥控器.如果你不是,你应该先阅读这个:https://devcenter.heroku.com/articles/git#multiple-remotes-and-environments

So, I recently did something similar, and found that the strategy below is the best option. I'm going to assume that you are familiar with git branching at this point, as well as Heroku remotes. If you aren't, you should read this first: https://devcenter.heroku.com/articles/git#multiple-remotes-and-environments

我采取的主要策略是拥有一个单一的代码库(一个单一的 Git 存储库):

The main strategy I'm taking is to have a single codebase (a single Git repo) with:

  • 一个 master 分支,其中包含您的所有共享代码:模板、视图、URL.
  • 许多基于 mastersite 分支,其中包含所有特定于站点的自定义:css、图像、设置文件(如果它们大不相同).
  • A master branch that contains all your shared code: templates, views, URLs.
  • Many site branches, based on master, which contain all site-specific customizations: css, images, settings files (if they are vastly different).

它的工作方式是这样的:

The way this works is like so:

首先,确保您在 master 分支上.

First, make sure you're on the master branch.

其次,为您的域之一创建一个新的 git 分支,例如:git checkout -b somedomain.com.

Second, create a new git branch for one of your domains, eg: git checkout -b somedomain.com.

第三,自定义您的 somedomain.com 分支,使其看起来像您想要的那样.

Third, customize your somedomain.com branch so that it looks the way you want.

接下来,通过运行 heroku create somedomain.com --remote somedomain.com,将 somedomain.com 实时部署到 Heroku.

Next, deploy somedomain.com live to Heroku, by running heroku create somedomain.com --remote somedomain.com.

现在,将您的 somedomain.com 分支代码推送到您的新 Heroku 应用程序:git push somedomain.com somedomain.com:master.这将在 Heroku 上部署您的代码.

Now, push your somedomain.com branch code to your new Heroku application: git push somedomain.com somedomain.com:master. This will deploy your code on Heroku.

现在您已经使用自己的 Heroku 应用程序部署了 somedomain.com 分支,您可以通过添加 --remote somedomain.com 来执行所有正常的 Heroku 操作到您的正常 Heroku 命令,例如:

Now that you've got your somedomain.com branch deployed with its own Heroku application, you can do all normal Heroku stuff by adding --remote somedomain.com to your normal Heroku commands, eg:

  • heroku pg:info --remote somedomain.com
  • heroku addons:add memcache:5mb --remote somedomain.com

所以,现在你基本上有两个分支:一个 master 分支和一个 somedomain.com 分支.

So, now you've basically got two branches: a master branch, and a somedomain.com branch.

回到你的 master 分支,为你的下一个域创建另一个新分支:git checkout master;git checkout -b anotherdomain.com.然后根据您的喜好对其进行自定义(css、特定于站点的内容),并按照我们上面所做的相同方式进行部署.

Go back to your master branch, and make another new branch for your next domain: git checkout master; git checkout -b anotherdomain.com. Then customize it to your liking (css, site-specific stuff), and deploy the same way we did above.

现在我相信你现在可以看到它的发展方向了.我们为每个自定义 domains 设置了一个 git 分支,并且每个域都有自己的 Heroku 应用程序.好处(显然)是这些项目自定义中的每一个都基于 master 分支,这意味着您可以轻松地一次更新所有站点.

Now I'm sure you can see where this is going by now. We've got one git branch for each of our custom domains, and each domain has it's own Heroku app. The benefit (obviously) is that each of these project customizations are based off the master branch, which means that you can easily make updates to all sites at once.

假设您更新了 master 分支中的一个视图——如何一次将其部署到所有自定义站点?轻松!

Let's say you update one of your views in your master branch--how can you deploy it to all your custom sites at once? Easily!

只需运行:

  • git checkout somedomain.com
  • git merge master
  • git push somedomain.com somedomain.com:master # 部署更改
  • git checkout somedomain.com
  • git merge master
  • git push somedomain.com somedomain.com:master # deploy the changes

并为您的每个域重复此操作.在我的环境中,我编写了一个脚本来执行此操作,但如果您愿意,可以轻松手动完成.

And repeat for each of your domains. In my environment, I wrote a script that does this, but it's easy enough to do manually if you'd like.

无论如何,希望这会有所帮助.

Anyhow, hopefully that helps.

这篇关于具有共享代码库和数据库的多个 Django 站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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