title: django multiple domains, single app
body: <p>I am trying to set my Django app work with multiple domains (while serving slightly different content)
  I wrote this middleware:</p>
  
  <pre><code>class MultiSiteMiddleware(object):
      def process_request(self, request):
          host = request.get_host()
          host_part = host.split(':')[0].split('.com')[0].split('.')
          host = host_part[len(host_part)-1] + '.com'
          site = Site.objects.get(domain=host)
          settings.SITE_ID = site.id
          settings.CURRENT_HOST = host
          Site.objects.clear_cache()
          return
  </code></pre>
  
  <p>In views I use this:</p>
  
  <pre><code>def get_site(request):
      current_site = get_current_site(request)
      return current_site.name
  
  def view(request, pk):
      site = get_site(request)
  
      if site == 'site1':
          # serve content1
          ...
      elif site == 'site2'
          # serve content2
          ...
  </code></pre>
  
  <p>But now there are 404 errors (I sometimes find them in logs, don't see them while browsing my site manually) where they aren't supposed to be, like my site sometimes is serving content for wrong domains, can they happen because of some flaw in the above middleware and view code or I should look somewhere else?</p>