Several sites in Django

Does anyone know how to add multiple domains in Django. I tried the following guides Several sites in one Django project with no luck.

My configuration looks like

Settings

/opt/django/project/settings.py

/opt/django/project/domain1_settings.py

Url

/opt/django/project/urls.py

/opt/django/project/domain1_urls.py

WSGI

/opt/django/project/domain1/domain1.wsgi

Apache

/etc/httpd/conf.d/django.conf

<VirtualHost * > ServerName domain1.co.uk ServerAlias www.domain1.co.uk direct.domain1.co.uk WSGIDaemonProcess domain1 processes=5 python-path=/usr/bin/python threads=1 WSGIScriptAlias / /opt/django/project/domain1/domain1.wsgi ErrorLog logs/domain1-error.log CustomLog logs/domain1-access.log common </VirtualHost> 

When I restart apache, I see no errors, but when I go to the site, I go to the domain (not django), which is configured mainly by httpd.conf.

Thanks,

+6
source share
1 answer

This answer assumes that you want to have two domain names, each of which runs separate Django projects, but is hosted on the same Apache server. If this is not the case, clarify your question!

First, you will need two VirtualHost entries in your apache conf (call domain1.co.uk and domain2.co.uk )

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

You will also need two wsgi files (two pointers in conf above)

 /opt/django/project/domain1/domain1.wsgi /opt/django/project/domain1/domain2.wsgi 

and will look something like

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

In settings.py make sure that both settings files have the difference SITE_ID = 1 or SITE_ID = 2 and point to the correct urls.py

 ROOT_URLCONF = 'urls' 

or

 ROOT_URLCONF = 'domain1_urls' 

Most of this question was gained from personal experience and this blog . Your project directories and domain names seem a bit confusing, I did my best to put them in the right places here, but you will need to adapt to your own goals.

Additional

If you have two sites running from the same server, you must be very careful to maintain consistency over project directories, static file directories and settings files, etc. From your question, you say that your settings files are /opt/django/project/settings.py and /opt/django/project/domain1_settings.py This is pretty confusing as it seems that you have one project directory ( /opt/django/project ). I would strongly recommend a stronger separation; as I described above, possibly installing your projects ( domain1 and domain2 ) in directories

 /opt/django/project/domain1/ /opt/django/project/domain2/ 

with corresponding settings files

 /opt/django/project/domain1/settings.py /opt/django/project/domain2/settings.py 

etc .. This should make it easier to determine what is going wrong.

+13
source

All Articles