“sites framework” on a single django instance

天涯浪子 提交于 2019-12-02 18:55:30

With stock Django you must have a unique settings.py for each site... because the SITE_ID is defined in settings.py and is the key for which site is handling this request.

In other words, SITE_ID is global to your instance and therefore you need an instance for each site.

You can have a common urls.py if you wish because there's nothing preventing you from using the same ROOT_URLCONF in all your site settings.py files... or you can have diffent one for each site. In this case you would want to include sub URLs to prevent repeating yourself for any common URLs.

There are at least two methods you can try to serve from a single instance:

  1. Use apache + mod_wsgi and use the WSGIApplicationGroup and/or WSGIProcessGroup directives. I've never needed these before so can't be completely sure these will work the way you want, but regardless you can definitely use mod_wsgi in daemon mode to greatly improve your memory footprint.

  2. You can play with Django middleware to deny/allow URLs based on the request hostname (see HttpRequest.get_host() in the Django docs). For that matter, even though it would be a slight performance hit, you can put a decorator on all your views that checks the incoming host.

FYI - I released django-dynamicsites which can be helpful with this issue - https://bitbucket.org/uysrc/django-dynamicsites/src

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