Multiple sites in a single Django project

Is it possible and correct to have several sites in one django project. So there will be a globally shared sints file, URL files along with globally shared "applications" for all sites and a common admin interface for all sites under a single django project. Each site can have its own settings, URLs and templates, which will be redefined or imported dynamically into the parent settings file.

Can someone tell me how to do this using Python2.6 + Django 1.2.1 + Apache2.2 + mod_wsgi. Since I'm a bit confused, the virtual host should be delivered using mod_wsgi.

In accordance with the above requirement, I plan to save the wsgi file in the main directory of the django project so that the main parameters are imported and in the main parameter I plan to dynamically import the specified site settings. Is this possible using Apache + mod_wsgi. Please advice.

OR

Instead of a global django project, I have to make a globally shared module for importing global settings and URLs into site settings and URLs.

I would like to improve code reuse, rather than making redundant changes on every site.

Please advice.

+4
source share
2 answers

Yes, it is quite possible. Sites can even share data.

The sites structure allows this - for documentation, see here:

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

+3
source

It is just like me. I borrowed from http://michal.karzynski.pl/blog/2010/10/19/run-multiple-websites-one-django-project/

Basically, you will create a virtualhost entry in the 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 will want to create two different wsgi files for each domain and place them in the directory where your project is located. 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 need to create two additional settings files ... so you will have

 settings.py domain1_settings.py domain2_settings.py 

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

example 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 will want to create 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.

+6
source

All Articles