Hi,

I am working on a project which needs to run any number of sites in
the same Django instance. Each site can be in many languages and must
have it's own set of templates and media. I have prototyped this using
the sites framework by setting the SITE_ID, MEDIA_ROOT and
TEMPLATE_DIRS at runtime in middleware in the process_request event
(see below). This is currently working perfectly for my requirements
but several sources suggest against altering the settings variables at
runtime. Can anyone suggest a more "best practice" approach or is this
really my only option? Having individual site settings files would not
work here as this needs to operate on a per-request basis. Also, it
wouldn't really be feasible for each site to have it's own Django
instance as this will be running in a load balanced environment and
would quickly become a real pain to manage and maintain.

Thanks in advance

Berry

My middleware (I've made minor alterations to the code for this
message so ignore any syntax errors):

class MultiSiteMiddleware:
    def process_request(self, request):
        settings.MEDIA_ROOT = settings.DEFAULT_MEDIA_ROOT

        if request.path.startswith('/admin/'):
            return

        try:
            host = request.get_host().rsplit(':', 1)[0]  # strip port
from hostname
            site = Site.objects.get(domain=host)
            site_profile = SiteProfile.objects.get(site_id=site.id)
            language_code = site_profile.default_language.code

            prefix = '%s/%s' % (site_profile.path_name, language_code)

            settings.SITE_ID = site.id
            settings.LANGUAGES = site_profile.language_list
            settings.MEDIA_ROOT = os.path.join(settings.PROJECT_PATH,
'media', 'sites', prefix)
            settings.TEMPLATE_DIRS =
(os.path.join(settings.PROJECT_PATH, 'templates', 'sites', prefix),)

            return

        except Site.DoesNotExist:
            return
HttpResponsePermanentRedirect(settings.NO_SITE_REDIRECT)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to