Ticket #4438: middleware.py

File middleware.py, 521 bytes (added by Max Battcher <me@…>, 8 years ago)

SetSiteFromHost middleware

Line 
1from django.conf import settings
2from django.contrib.sites.models import Site
3
4"""
5Site-related Middleware
6"""
7
8class SetSiteFromHost:
9    """
10    Change the settings.SITE_ID to match the request.META['HTTP_HOST']
11    """
12
13    def process_request(self, request):
14        try:
15            site = Site.objects.get(domain=request.META['HTTP_HOST'])
16        except Site.DoesNotExist:
17            # Keep existing setting
18            return
19        settings.SITE_ID = site.id
20
21# vim: ai ts=4 sts=4 et sw=4
Back to Top