"sites framework" on a single django instance
Asked Answered
P

2

6

I want to serve up specialized RSS feeds on a different subdomain from the rest of the site.

Can I use the sites framework to use a different urls.py and settings.py file within a single django instance. or do I need to set up two apache locations and just set the different settings.py files in the apache conf.

The reason I need to set up two urls.py files is to avoid duplicate content. I don't want the main site to be available at rss.example.com and I don't want the specialized feeds to be accessible on example.com

Serving them from a single django instance would be ideal because we're on shared hosting with limited memory, and it seems like such a waste to have an instance open that only serves up rss.

edit: I concluded that multiple instances with seperate urls.py files would be easiest for me... but I found this article describing how to do it using a single instance:

http://effbot.org/zone/django-multihost.htm

Solution: Django tupperware

I ended up writing a framework for running multiple copies of a site on a single django instance.

The basic idea is to change out the SITE_ID setting on the fly for each request and load alternate settings from the database. It does this based on domain and uses SITE_ID = 1 by default (when it can't find anything)

All settings in the settings.py file act as defaults which are overridden by the settings stored in the database for the current site.

It works pretty well :) and it's running in production at http://rootbuzz.com

Plateau answered 17/6, 2009 at 13:27 Comment(5)
Are you still use tupperware? Or did you find any better and fresh alternatives?Noway
@MuratCorlu Tupperware is still in production use on that project :)Plateau
I tried to use it with Django 1.7 but it didn't worked as expected. Also project seems dead on Bitbucket. Can you share an example configuration about how did you use tupperware?Noway
@MuratCorlu Django 1.7 has made a lot of changes - I haven't tested with 1.7 - and from my experience updating apps to 1.7, I'd expect it not to work. I can confirm that it does work (and is working) on django 1.4 (a LTS release, still supported)Plateau
@MuratCorlu for what it's worth, on all applications after creating tupperware I have opted not to use "reusable" apps and stored all configuration data in the database the old-fashioned way: create a new model to store the configuration infoPlateau
P
10

With stock Django you must have a unique settings.py for each site... because the SITE_ID is defined in settings.py and is the key for which site is handling this request.

In other words, SITE_ID is global to your instance and therefore you need an instance for each site.

You can have a common urls.py if you wish because there's nothing preventing you from using the same ROOT_URLCONF in all your site settings.py files... or you can have diffent one for each site. In this case you would want to include sub URLs to prevent repeating yourself for any common URLs.

There are at least two methods you can try to serve from a single instance:

  1. Use apache + mod_wsgi and use the WSGIApplicationGroup and/or WSGIProcessGroup directives. I've never needed these before so can't be completely sure these will work the way you want, but regardless you can definitely use mod_wsgi in daemon mode to greatly improve your memory footprint.

  2. You can play with Django middleware to deny/allow URLs based on the request hostname (see HttpRequest.get_host() in the Django docs). For that matter, even though it would be a slight performance hit, you can put a decorator on all your views that checks the incoming host.

Precise answered 17/6, 2009 at 17:11 Comment(3)
Since this subdomain only serves specialized RSS feeds, I'm going to set maxRequestsPerChild to 1 to conserve RAM. Any downsides I should know about (other than the speed penalty)Plateau
PS - As you can see I just decided to make a new instance... I have FAR too many views to be putting a decorator on every single onePlateau
Cool, I'm about to take the dive into the sites framework. This is helpful for my research.Pervious
M
3

FYI - I released django-dynamicsites which can be helpful with this issue - https://bitbucket.org/uysrc/django-dynamicsites/src

Myelitis answered 27/3, 2011 at 1:42 Comment(1)
thanks for the update... I also wrote something like this: bitbucket.org/jiaaro/django-tupperware/srcPlateau

© 2022 - 2024 — McMap. All rights reserved.