tags:

views:

21

answers:

1

I have a single application which manages and delivers several sites. I does this by determining which domain to serve from the URL:

http://myapplication.com/site_1/ or http://myapplication.com/site_2/

Each site obviously has it's own pages, so might end up with a url like this:

http://myapplication.com/site_1/contact_us/

What I'd like to do is create some middleware which, when a url such as the two above (http://myapplication.com/somedomainhere) it will check the existence of that particular domain in a database (which I have already created) and, if it exists, continue to the index view. If the domain doesn't exist, I want to divert to a 404 page.

Is this something which is relatively simple to do with middleware and if so, does anyone have any examples of how I might go about accomplishing this?

I am aware of the Sites framework which comes with Django, but I will be using the above to create something slightly different.

+1  A: 
    class MyMiddleware():
    def process_request(self, request):
        app_name = request.path.split('/')[0]

        try:
            app = Apps.objects.get(name=app_name)
        except DoesNotExist:
            return

        request.urlconf = app.urlconf
        return

The include your middleware in your settings.py with all the rest.

This assumes that your app model includes a property that knows about the urlconf for that app. Just point the request at that urlconf, and Django takes care of the rest.

For reference, see the documentation on middleware, request processing and setting the urlconfs.

Hope this helps!

godswearhats
Thanks very much. This is round about what I want, but I'm a little stumped by the app.urlconf part, what exactly should I be returning from within my model?
Hanpan
http://docs.djangoproject.com/en/1.2/ref/request-response/#django.http.HttpRequest.urlconf explains how the request.urlconf is used. Hope that helps.
godswearhats