Django中的多个站点 [英] Multiple Sites in Django

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

问题描述

有没有人知道如何添加多个域到Django。
我试过按照这里的指南单个Django项目下的多个站点没有运气



我的配置看起来像这样



设置 / p>

/opt/django/project/settings.py



/opt/django/project/domain1_settings.py



网址



/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
< /虚拟主机>

当我重新启动apache时,我看到没有错误,但是当我去网站时,我被发送到非django)域在主httpd.conf中配置。



谢谢,

解决方案

这个答案假设你想要有两个域名,每个运行单独的Django项目,但是从同一个Apache服务器托管。如果不是这样,请改善你的问题!



要开始,您需要在您的apache conf中使用两个 VirtualHost 条目(让我们调用您的网站 domain1.co.uk domain2.co.uk

 #虚拟主机设置
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中指向两个)

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

,看起来像

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

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

settings.py 确保两个设置文件有区别 SITE_ID = 1 SITE_ID = 2 ,并指出正确的 urls.py

  ROOT_URLCONF ='urls'

  ROOT_URLCONF ='domain1_urls'

这个问题的大部分内容来自个人e经验和这个博客帖子。您的项目目录和域名似乎有点混乱,我已尽全力将其纳入正确的位置,但您需要根据自己的需要进行调整。



其他



如果您有两个网站从同一台服务器运行,则必须非常小心以保持项目的一致性目录,静态文件目录和设置文件等。
从你的问题你说你的设置文件是 /opt/django/project/settings.py /opt/django/project/domain1_settings.py 这很令人困惑,因为您似乎有一个项目目录( / opt / django / project )。我强烈建议加强分离;如上所述,可能在目录中设置您的项目( domain1 domain2

  / opt / django / project / domain1 / 
/ opt / django / project / domain2 /
/ pre>

与相应的设置文件

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

等。这应该可以更容易地发现哪里出了问题。


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.

My configuration looks like this

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 am sent to the (non django) domain that is configured within the main httpd.conf.

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

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

/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

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

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.

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

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