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

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

问题描述

我为20个不同的国家创建了一个具有20个站点(每个站点不同域名)的Django项目。这些网站共享一切:代码库,数据库,网址,模板等。



他们唯一不分享的是小型定制(CSS主题背景颜色,语言代码等),我在每个站点设置文件(每个站点都有一个设置文件,所有这些文件导入全局设置文件与常见的东西)中设置。现在,为了在开发模式下运行网站,我可以这样做:

  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个应用程序。是否有解决方案?

谢谢!

解决方案

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



我正在采取的主要策略是拥有一个单一的代码库单个Git repo)与:




  • A master 分支包含所有共享代码: c code code code code code $ c >,其中包含所有特定于站点的自定义:css,图像,设置文件(如果它们有很大的不同)。



作品如下所示:



首先,确保您位于 master 分支。



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



第三,自定义您的 somedomain.com 分支,使它看起来像y



接下来,通过运行 heroku将 somedomain.com 直播部署到Heroku创建somedomain.com - 远程somedomain.com



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



现在你已经有了你的 somedomain.com 自己的Heroku应用程序,您可以通过添加 - 远程somedomain.com 到您正常的Heroku命令,例如:




  • heroku pg:info --remote somedomain.com

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

  • 等。



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



返回到您的 master 分支,并为您的下一个域创建另一个新分支: git checkout master; git checkout -b anotherdomain.com 。然后根据您的喜好(css,特定于网站的内容)进行自定义,并以与上述相同的方式进行部署。



现在我确定你可以看到这是现在去我们为每个自定义的分配了一个git分支,每个域都有自己的Heroku应用程序。好处(显然)这些项目定制中的每一个都是基于 master 分支,这意味着您可以轻松地更新所有网站一个。



假设您在 master 分支中更新您的一个视图 - 如何部署您的自定义网站一次 只需运行




  • git checkout somedomain.com

  • git merge master

  • git push somedomain.com somedomain.com:master #部署更改



并重复为您的每个域。在我的环境中,我写了一个这样做的脚本,但是如果你愿意的话,手动做很容易。



无论如何,希望有帮助。

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.

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

I have a couple of questions:

  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?

Thanks!

解决方案

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

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

  • 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:

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

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

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

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

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.

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
  • etc.

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

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.

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.

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!

Just run:

  • 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天全站免登陆