Django to use different settings.py file based on subdomains

我与影子孤独终老i 提交于 2019-12-05 03:56:39

问题


How can Django use different settings.py file based on subdomains.

Can these utilities ("django-admin", "python manage.py") still be used if there were different settings connecting to different databases.


回答1:


ok you have two dimensions you need to cover with your settings:

  1. Domain (site)
  2. Current Machine

Here is what I recommend:

universal_settings.py - all the settings you want to inherit everywhere (all machines, all domains)

local_settings.py - settings on a per machine basis (database settings, mail server, etc)

site_1.py - settings that are specific to a one of your domains
site_2.py - settings that are specific to a one of your domains
site_n.py - you get the idea

the bottom of universal_settings.py should include:

from local_settings import *

This will override anything in the universal settings as necessary.

similarly, each of the site_1.py, site_2.py, site_n.py settings files should begin with:

from universal_settings import *

Finally you need to set up an apache (or nginx, or whatever) instance for each domain and use the appropriate site_n.py as the settings file for that server

This is the method that works best for me :)



来源:https://stackoverflow.com/questions/1866760/django-to-use-different-settings-py-file-based-on-subdomains

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