Multiple Sites under single Django project

后端 未结 2 1533
太阳男子
太阳男子 2021-01-31 23:41

Is it possible and correct to have multiple sites under single django project. So that there will be globally shared sittings file,urls files along with global shared \'apps\' f

相关标签:
2条回答
  • 2021-02-01 00:26

    This following is the same way that I did it. I borrowed from http://michal.karzynski.pl/blog/2010/10/19/run-multiple-websites-one-django-project/

    Basically, you'll create a virtualhost entry on your http.conf file for each domain.

    # Virtual hosts setup
    NameVirtualHost *
    <VirtualHost *>
        ServerName example1.com
    
        WSGIDaemonProcess APPLICATION_NAME processes=5 python-path=/home/USERNAME/webapps/APPLICATION_NAME:/home/USERNAME/webapps/APPLICATION_NAME/lib/python2.6 threads=1
        WSGIScriptAlias / /home/USERNAME/webapps/APPLICATION_NAME/domain1.wsgi
    </VirtualHost>
    
    <VirtualHost *>
        ServerName example2.com
    
    
        WSGIDaemonProcess APPLICATION_NAME_www processes=5 python-path=/home/USERNAME/webapps/APPLICATION_NAME:/home/USERNAME/webapps/APPLICATION_NAME/lib/python2.6 threads=1
        WSGIScriptAlias / /home/USERNAME/webapps/APPLICATION_NAME/domain2.wsgi
    </VirtualHost>
    

    Then you'll want to create two different wsgi files for each domain and put them in the directory that holds your project. The WSGIScriptAlias is the path to the wsgi file so make sure they are the same...

    Example Wsgi file:

    import os
    import sys
    from django.core.handlers.wsgi import WSGIHandler
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECT_NAME.domain1_settings' # or PROJECT_NAME.domain2_settings
    application = WSGIHandler()
    

    Then you'll want to create two extra settings files... so you'll have

    settings.py
    domain1_settings.py
    domain2_settings.py
    

    domain1_settings.py and domain2_settings.py will import settings.py:

    example of domain1_settings.py:

    from settings import *
    
    SITE_ID = 1
    
    ROOT_URLCONF = 'domain1_urls'
    
    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
    
    # other apps specific to this domain
    )
    

    Finally, you'll want to create the two separate urls files..

    domain1_urls.py and domain2_urls.py

    domain1_urls.py will be the default for site_id 1, and domain2_urls.py will be the default for site_id 2.

    0 讨论(0)
  • 2021-02-01 00:33

    Yes, this is entirely possible. The sites can even share data.

    The sites framework enables this - for documentation, see here:

    https://docs.djangoproject.com/en/1.11/ref/contrib/sites/

    0 讨论(0)
提交回复
热议问题