在Django多个站点 [英] Multiple Sites in Django

查看:228
本文介绍了在Django多个站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何将多个域添加到Django的。
我试着在这里<一个跟随导游href=\"http://stackoverflow.com/questions/3369486/multiple-sites-under-single-django-project\">Multiple下单Django项目的网站,没有运气。

Does anyone know how to add multiple domains to Django. I've tried following the guides here Multiple Sites under single Django project with no luck.

我的配置看起来像这样

设置

/opt/django/project/settings.py

/opt/django/project/settings.py

/opt/django/project/domain1_settings.py

/opt/django/project/domain1_settings.py

网址

/opt/django/project/urls.py

/opt/django/project/urls.py

/opt/django/project/domain1_urls.py

/opt/django/project/domain1_urls.py

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>

当我重新启动Apache我没有看到错误,但是当我去到现场,我发送到在主httpd.conf文件中配置的(非Django的)域。

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.

谢谢,

推荐答案

这答案让你想拥有的每个运行单独的Django项目的二级域名的假设,但是从同一个Apache服务器托管。如果不是的话,请精炼您的问题!

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. If this isn't the case, please refine your question!

要开始,你需要在你的apache的conf两个虚拟主机项(我们称之为您的网站 domain1.co.uk domain2.co.uk

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)

# 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>

您还需要2 WSGI文件(指两个以上的conf)

You'll also need 2 wsgi files (pointed two in the conf above)

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

和看起来像

import os
import sys
from django.core.handlers.wsgi import WSGIHandler

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings' 
# or     project.domain1_settings
application = WSGIHandler()

走上 settings.py 确保两个设置文件有差异 SITE_ID = 1 = SITE_ID 2 和你指向正确的 urls.py

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

ROOT_URLCONF = 'urls'

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.

其他

如果您有来自同一个服务器上运行两个网站,你必须非常小心,以保持对项目目录,静态文件目录和设置文件等的一致性
从你的问题,你说你的设置文件的 /opt/django/project/settings.py /opt/django/project/domain1_settings.py 这是相当混乱,因为它似乎你有一个项目目录(的/ opt / Django的/项目)。我会强烈建议更强的分离;正如我上面描述,可能设置你的项目( DOMAIN1 DOMAIN2 )的目录

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). I would strongly recommend stronger separation; as I describe above, maybe setting your projects (domain1 and domain2) in directories

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

相应设置文件

/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.

这篇关于在Django多个站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆