问题
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:
- Domain (site)
- 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 domainssite_2.py
- settings that are specific to a one of your domainssite_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