Is it safe to change settings.SITE_ID from middleware in Django?

I modified the multihost.py middleware that I found at http://effbot.org/zone/django-multihost.htm to set the parameters. SITE_ID is dynamic, but there are some concerns that I may have simply left a disclaimer.

Most of the examples I found for several domain hosting services were configured with several settings.py files hardcoded to the corresponding SITE_ID.

I created a fatal error fix here? Changes this value, dynamically bites me on a **.

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

class MultiHostMiddleware:

    def process_request(self, request):
        try:
            host_raw = request.META["HTTP_HOST"]
            colon = host_raw.find(':')
            if colon > -1:
                host = host_raw[0:colon]
            else:
                host = host_raw

            s = Site.objects.get(domain=host)
            if s:
                settings.SITE_ID = s.id

        except KeyError:
            pass # use default urlconf (settings.ROOT_URLCONF)

For the curious, this still works, but does not withstand real traffic.

+5
1

, , .

, . ; sleep() , HttpResponse . , .

prefork, , . matplotlib, , matplotlib.rcParams.update(). prefork fcgi, , (, , , ).

. , , , RequestSite, sites, django-registration, , RequestSite , .

+6

All Articles