Yo estoy usando mi propia version de sites para permitir varios dominios por sitios, etc, y que cambie el SITE_ID de forma dinamica, para subdominios te sirve igualmente:
El middleware:
Código:
#-*- coding: UTF-8 -*-
from django.conf import settings
from sitiosweb.models import SitioWeb, DominioDNS
from os.path import join
class SetDynamicSites(object):
""" Change SITE_ID dinamics. """
def process_request(self, request):
try:
current_site = DominioDNS.objects.get(dominio=request.get_host()).sitioweb
settings.SITE_ID = current_site.id
settings.MEDIA_URL = '//' + request.get_host() + settings.MEDIA_URL_PREF
settings.STATIC_URL = '//' + request.get_host() + settings.STATIC_URL_PREF
settings.STATIC_ROOT = join(settings.STATIC_ROOT_DIR, str(current_site.id))
settings.MEDIA_ROOT = join(settings.STATIC_ROOT_DIR, str(current_site.id))
settings.HOST = request.get_host()
settings.ALLOWED_HOSTS = []
for dominio in current_site.dominiodns_set.all():
settings.ALLOWED_HOSTS.append(dominio.dominio)
except:
settings.SITE_ID = 1
return None
Los modelos:
Código:
class SitioWeb(models.Model):
nombre = models.CharField(max_length=512, blank=False)
def __str__(self):
return self.nombre
class DominioDNS(models.Model):
dominio = models.CharField(max_length=256, unique=True, blank=False)
sitioweb = models.ForeignKey('SitioWeb')
def __str__(self):
return self.dominio + ' - ' + self.sitioweb.nombre