简体   繁体   English

Django-CMS:同一项目中的多个域

[英]Django-CMS: Multiple domains on same project

i'm trying to run django-cms on two different domains. 我正试图在两个不同的域上运行django-cms。 For that i created two domains (django.contrib.sites) and added to them django-cms pages. 为此,我创建了两个域(django.contrib.sites)并添加了django-cms页面。 Now i created a SiteDetectionMiddleware: 现在我创建了一个SiteDetectionMiddleware:

class SiteDetectionMiddleware:
  def process_request(self, request):
    settings.SITE_ID = 1
    host = request.META.get('HTTP_HOST')
    if host:
      try:
        site = Site.objects.get(domain=host)
        settings.SITE_ID = site.id
      except Site.DoesNotExist:
        pass

It seems to work correctly, when i call the website in browser for the first time after restarting apache. 它似乎工作正常,当我重新启动apache后第一次在浏览器中调用网站时。 Then i changed to the other site and got a NoReverseMatch Error. 然后我改为其他网站并得到NoReverseMatch错误。

Does anyone have an idea what could be wrong? 有谁知道什么可能是错的?

I thought this should work out of the box in django-cms? 我认为这应该在django-cms开箱即用?

regards Colin 科林

Why are you setting the SITE_ID statically? 为什么要静态设置SITE_ID You should probably create two settings files and use some form of inheritance depending on project differentiation, eg: 您应该创建两个设置文件,并根据项目区别使用某种形式的继承,例如:

local_settings.py (not under version control holds sensitive data like database passwords and the secret key) local_settings.py (不在版本控制下保存敏感数据,如数据库密码和密钥)

SECRET_KEY = 'as!sfhagfsA@$1AJFS78787124!897zR81'

settings.py (holds settings that are equal for both sites) settings.py (保存两个站点相同的设置)

# preferably at the bottom
try:
    from local_settings import *
except ImportError:
    pass

settings_foo.py (holds settings specific to site 1) settings_foo.py (保存特定于站点1的设置)

from settings import *

SITE_ID = 1

settings_bar.py (holds settings specific to site 2) settings_bar.py (保存特定于站点2的设置)

from settings import *

SITE_ID = 2

settings_deployment_foo.py (overwrites variables for production) settings_deployment_foo.py (覆盖生产的变量)

from settings_foo import *

DEBUG = False

settings_deployment_bar.py (overwrites variables for production) settings_deployment_bar.py (覆盖生产的变量)

from settings_bar import *

DEBUG = False

Then just create two sites within admin/sites or use a fixture (assuming you are sharing a database cross these projects you'll only have to do this once). 然后只需在admin/sites创建两个站点或使用fixture(假设您通过这些项目共享数据库,您只需要执行一次)。

If your languages are same for all domain like xyz.com and abc.com 如果您的语言对于xyz.comabc.com等所有域都相同

So you can handle it in middleware, so middleware can assign the available languages to subdomains at runtime. 因此,您可以在中间件中处理它,因此中间件可以在运行时将可用语言分配给子域。

from django.conf import settings
from django.contrib.sites.models import Site

class SiteMiddleware(object):
    def process_request(self, request):
        try:
            current_site = Site.objects.get(domain=request.get_host())
        except Site.DoesNotExist:
            current_site = Site.objects.get(id=settings.DEFAULT_SITE_ID)

        request.current_site = current_site
        settings.SITE_ID = current_site.id
        settings.SITE_NAME = current_site.name
        if settings.SITE_ID is not 1:
            settings.CMS_LANGUAGES[settings.SITE_ID] = settings.CMS_LANGUAGES[1]

After few hours trial and error, I got the following solution. 经过几个小时的反复试验,我得到了以下解决方案。

We need to keep relation of the between SITE and CMS_LANGUAGES 我们需要保持SITECMS_LANGUAGES之间的关系

For example, I have two sites abc.com with site ID 1 and xyz.com with site ID 2 例如,我有两个站点ID为1的站点abc.com ,站点ID为2的xyz.com

so you need to mentioned the following relationship in settings.py 所以你需要在settings.py提到以下关系

CMS_LANGUAGES = {
        ## Customize this
        'default': {
            'public': True,
            'hide_untranslated': False,
            'redirect_on_fallback': True,
        },
        1: [
            {
                'public': True,
                'code': 'en',
                'hide_untranslated': False,
                'name': gettext('en'),
                'redirect_on_fallback': True,
            },
            {
                'public': True,
                'code': 'zh',
                'hide_untranslated': False,
                'name': gettext('zh'),
                'redirect_on_fallback': True,
            },
            {
                'public': True,
                'code': 'my',
                'hide_untranslated': False,
                'name': gettext('my'),
                'redirect_on_fallback': True,
            },
        ],
        2: [
            {
                'public': True,
                'code': 'en',
                'hide_untranslated': False,
                'name': gettext('en'),
                'redirect_on_fallback': True,
            },
            {
                'public': True,
                'code': 'zh',
                'hide_untranslated': False,
                'name': gettext('zh'),
                'redirect_on_fallback': True,
            },
            {
                'public': True,
                'code': 'my',
                'hide_untranslated': False,
                'name': gettext('my'),
                'redirect_on_fallback': True,
            },
        ],
    }

Also I am using the site middleware, which detect the site id using domain name. 我也使用site中间件,它使用域名检测网站ID。

I hope it helps someone :) 我希望它有助于某人:)

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

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