3

i'm trying to run django-cms on two different domains. For that i created two domains (django.contrib.sites) and added to them django-cms pages. Now i created a 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. Then i changed to the other site and got a NoReverseMatch Error.

Does anyone have an idea what could be wrong?

I thought this should work out of the box in django-cms?

regards Colin

cwirz
  • 404
  • 9
  • 24

3 Answers3

6

Why are you setting the SITE_ID statically? You should probably create two settings files and use some form of inheritance depending on project differentiation, e.g.:

local_settings.py (not under version control holds sensitive data like database passwords and the secret key)

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

settings.py (holds settings that are equal for both sites)

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

settings_foo.py (holds settings specific to site 1)

from settings import *

SITE_ID = 1

settings_bar.py (holds settings specific to site 2)

from settings import *

SITE_ID = 2

settings_deployment_foo.py (overwrites variables for production)

from settings_foo import *

DEBUG = False

settings_deployment_bar.py (overwrites variables for production)

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

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • 1
    @Hedde van der Heide how can we load the respective settings.py for each site dynamically based on the typed url. – bhushya Mar 03 '15 at 10:44
  • @Wirzi how do u load the `settings_foo.py` and `settings_bar.py` dynamically based on site id? – bhushya Mar 03 '15 at 11:53
  • Use the wsgi.py file to load different settings files for each domain. – cwirz Mar 04 '15 at 13:25
  • I use multiple VirtualHost in the apache httpd.conf file and load the for example a foo_wsgi.py for one domain and a bar_wsgi.py for the other one. Live example: https://gourmendo.com and https://blog.gourmendo.com – cwirz Mar 04 '15 at 13:28
  • more infos about this problem: https://groups.google.com/forum/#!topic/django-cms/jQNWVos9vg8 – cwirz Mar 04 '15 at 14:47
4

If your languages are same for all domain like xyz.com and abc.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]
Shabbir
  • 86
  • 3
1

After few hours trial and error, I got the following solution.

We need to keep relation of the between SITE and CMS_LANGUAGES

For example, I have two sites abc.com with site ID 1 and xyz.com with site ID 2

so you need to mentioned the following relationship in 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.

I hope it helps someone :)

bhushya
  • 1,317
  • 1
  • 19
  • 33