简体   繁体   中英

Serving Multiple Domains in Django

I currently have 2 domains that I want to share a single Django project via the use of Apache (and wsgi) I've used the following guide that I have found here: http://www.fir3net.com/Django/how-to-serve-multiple-domains-from-within-a-single-django-project.html

To note: Domain 1 isn't using a database but domain2 is. And I currently have a single database configured within the main settings.py file.

The issue I first had is that the template for domain1 was found but for domain2 it was unable to locate it. After some troubleshooting I added the TEMPLATE_DIRs to the domain2_settings.py file. Even though I would of expected this to be picked up by the main settings.py file. Now I am getting an error that domain2 is unable to find a database and from the debug output it shows that there is no database assigned. Even though I would of expected the database settings to be be pulled from the main settings.py file.

Heres a summary of my layout:

/opt/
`-- django
    |
    `-- myproject
        |-- __init__.py
        |-- domain1
        |   |-- __init__.py
        |   |-- domain1.wsgi
        |   |-- domain1_settings.py
        |   |-- domain1_urls.py
        |   |-- models.py
        |   |-- tests.py
        |   |-- views.py
        |-- domain2
        |   |-- __init__.py
        |   |-- domain2.wsgi
        |   |-- domain2_settings.py
        |   |-- domain2_urls.py
        |   |-- models.py
        |   |-- tests.py
        |   |-- views.py
        |-- manage.py
        |-- settings.py
        |-- templates
        |   |-- domain1-base.html
        |   |-- domain2-base.html
        `-- urls.py

settings.py

[root@william myproject]# cat settings.py
# Django settings for myproject project.

DEBUG = False
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '123',                      # Or path to database file if using sqlite3.
        'USER': '123',                      # Not used with sqlite3.
        'PASSWORD': '##########',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': ''                      # Set to empty string for default. Not used with sqlite3.
    },
}

domain2_settings.py

[root@william myproject]#  cat domain2/domain2_settings.py
from settings import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

SITE_ID = 2

ROOT_URLCONF = 'domain2.domain2_urls'

TEMPLATE_DIRS = (
        "/opt/django/myproject/templates"
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'domain2',
)

Am I going about this the correct way (ie multiple domains, single project, apache and using a single database)?

The issue was I still had a settings.py and urls.py file within the domain2 folder. Once I removed these the issue was resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM