4

Consider a Django app built to serve multiple sites with slightly differing content using the standard Django sitesframework.

The traditional way to host this would be to configure multiple Site objects and setup the app in multiple Django projects with each project pointing to a different SITE_ID in their respective settings.py:s.

For various reasons I'd like to avoid having to create a new project for each new site. I want to be able to setup one project and have Django figure out which Site object to use based on the hostname referenced in the incoming HTTP request.

What is the recommended way to achieve this functionality?

Clarification: I want the site framework to ignore settings.SITE_ID (which is hard-coded in settings.py) and instead dynamically fetch Site objects based on what is in the Host header. Why this requirement? I'll be adding and removing sites multiple times per hour and the total amount of sites will exceed 10,000, so setting up a Django project for each site is not an option. Is this a problem that is solvable in Django? If so, what is the best way to achieve it?

knorv
  • 49,059
  • 74
  • 210
  • 294

1 Answers1

2

The recommended way is to not attempt it at all, since settings should never change at runtime. Instead, set a variable in your virtual host configuration and have the WSGI adapter script or settings module pick one of the sites based on that.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Please note that I don't want the setting (SITE_ID) to change at runtime - all I want is to make the sites module work dynamically based on the hostname header. That is picking the Site object dynamically based on what is in the request's hostname header. Are you implying that the is no correct way to achieve that? – knorv Jul 11 '11 at 00:36
  • The `Host` header doesn't come in until there's a request. This happens, by definition, at runtime. – Ignacio Vazquez-Abrams Jul 11 '11 at 03:18
  • That is obviously so. What I meant is that I want the site framework to ignore settings.SITE_ID (which is obviously hard-coded) and instead dynamically fetch Site objects based on what is in the "Host" header. Since I'll be adding and removing sites hourly I do not want to setup a Django project for each site. Is this a problem that is solvable in Django? If so, what is the best way to achieve it? – knorv Jul 11 '11 at 16:20
  • Have you found a solution to your problem? Thanks! – e-Jah Sep 18 '11 at 16:09