How to host 50 domains/sites with common Django code base

最后都变了- 提交于 2020-01-14 03:01:06

问题


I have 50 different websites that use the same layout and code base, but mostly non-overlapping data (regional support sites, not link farm). Is there a way to have a single installation of the code and run all 50 at the same time?

When I have a bug to fix (or deploy new feature), I want to deploy ONE time + 1 restart and be done with it.

Also:

Code needs to know what domain the request is coming to so the appropriate data is displayed.


回答1:


The Sites framework comes to mind.

Apart from that we have Django running for multiple sites by symlinking Django to various docroots. Works like a charm, too.




回答2:


I can see two quite distinct ways to do this:

  1. Use one database and the sites framework. Every post/picture/whatever model is connected to a Site and you always filter on Site. This requires a separate settings file for every database.
  2. Use one database for each and every site. This allows different users for every site, but requires duplication of everything that is stored in the database. It also requires a separate settings file pointing to the correct database.

Either way, you do not duplicate any code, only data.

--

If you need to do site-specific, or post-specific changes to ie. a template, you should read up on how Django loads templates. It allows you to specify a list, ie ["story_%d.html", "story_site_%d.html", "story.html"] and django will look for the templates in that order.




回答3:


I just ran into this and ended up using a custom middleware class that:

  1. Fetch the HTTP_HOST
  2. Clean the HTTP_HOST (remove www, ports, etc.)
  3. Look up domain in a Website table that's tied to each account.
  4. Set the account instance on the HTTPRequest object.

The throughout my view code I do lookups based on the account stored in the HTTPRequest objects.

Hope that helps someone in the future.



来源:https://stackoverflow.com/questions/2655548/how-to-host-50-domains-sites-with-common-django-code-base

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!