简体   繁体   English

Django中的多个站点

[英]Multiple Sites in Django

Does anyone know how to add multiple domains to Django. 有谁知道如何将多个域添加到Django。 I've tried following the guides here Multiple Sites under single Django project with no luck. 我已经尝试按照单个Django项目下的多个站点的指南,没有运气。

My configuration looks like this 我的配置看起来像这样

Settings 设置

/opt/django/project/settings.py /opt/django/project/settings.py

/opt/django/project/domain1_settings.py /opt/django/project/domain1_settings.py

Url 网址

/opt/django/project/urls.py /opt/django/project/urls.py

/opt/django/project/domain1_urls.py /opt/django/project/domain1_urls.py

wsgi WSGI

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

Apache 阿帕奇

/etc/httpd/conf.d/django.conf /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 am sent to the (non django) domain that is configured within the main httpd.conf. 当我重新启动apache时,我看到没有错误,但是当我访问该站点时,我被发送到主httpd.conf中配置的(非django)域。

Thanks, 谢谢,

This answer makes the assumption that you want to have two domain names each running separate Django projects, but being hosted from the same Apache server. 这个答案假设您希望有两个域名,每个域名运行单独的Django项目,但是从同一个Apache服务器托管。 If this isn't the case, please refine your question! 如果不是这样,请优化您的问题!

To start with you'll need two VirtualHost entries in your apache conf (let's call your sites domain1.co.uk and domain2.co.uk ) 首先,您需要在apache conf中使用两个VirtualHost条目(让我们将您的网站称为domain1.co.ukdomain2.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'll also need 2 wsgi files (pointed two in the conf above) 你还需要2个wsgi文件(在上面的conf中指向两个)

/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()

Onto the settings.py make sure that both settings files have difference SITE_ID = 1 or SITE_ID = 2 and that you point to the correct urls.py settings.py确保两个设置文件的区别SITE_ID = 1SITE_ID = 2并指向正确的urls.py

ROOT_URLCONF = 'urls'

or 要么

ROOT_URLCONF = 'domain1_urls'

Much of this question has been sourced from personal experience and this blog post. 这个问题的大部分来自个人经验和这篇博客文章。 Your project directories and domain names seem to be a little confusing, I've done my best to fit them into the correct places here, but you will need to adjust for your own purposes. 您的项目目录和域名似乎有点令人困惑,我已尽力将它们放入正确的位置,但您需要根据自己的目的进行调整。

Additional 额外

If you have two sites running from the same server, you will have to be very careful to maintain consistency over project directories, static file directories and settings files etc. From your question you say your settings files are /opt/django/project/settings.py and /opt/django/project/domain1_settings.py This is quite confusing as it seems that you have one project directory ( /opt/django/project ). 如果您有两个站点在同一服务器上运行,则必须非常小心地保持项目目录,静态文件目录和设置文件等的一致性。从您的问题中,您说您的设置文件是/opt/django/project/settings.py/opt/django/project/domain1_settings.py这很令人困惑,因为你似乎有一个项目目录( /opt/django/project )。 I would strongly recommend stronger separation; 我强烈建议加强分离; as I describe above, maybe setting your projects ( domain1 and domain2 ) in directories 如上所述,可能在目录中设置项目( domain1domain2

/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 spot what is going wrong where. 这应该可以更容易地发现哪里出了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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