Django

Code

Ticket #5022 (closed: wontfix)

Opened 1 year ago

Last modified 1 year ago

Proposed middleware: SubdomainURLsMiddleware

Reported by: tzellman@gmail.com Assigned to: nobody
Milestone: Component: Uncategorized
Version: SVN Keywords: enhancement proposal subdomain domain urlconf
Cc: drackett@mac.com Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

I'm developing an application and want to support subdomains. What I had in mind was the ability to support different views depending on the subdomain hit. Essentially, what it comes down to is I want the ability to support separate URL configurations (and thus views) for certain subdomains (known ahead of time). For example, I may have a mobile.mysite.com, where the 'mobile' subdomain deals with urls/views meant for mobile phones.

I noticed issue #4438, and it wasn't exactly what I wanted, or the right solution, so I started researching.

I noticed that in BaseHandler::get_response it attempts to get the 'urlconf' attribute from the request. So, I created a middleware that figures out the subdomain (some code borrowed from http://www.rossp.org/blog/2007/apr/28/using-subdomains-django/), and matches it against a subdomain->urlconf dictionary in settings.py. And voila! It works! Attached is the file that with the proposed solution.

I am open for critiques/discussion of this topic.

Thanks, Tom Zellman

Attachments

middleware.py (1.1 kB) - added by tzellman@gmail.com on 07/30/07 22:43:47.
SubdomainURLsMiddleware
middleware.2.py (0.8 kB) - added by tzellman@gmail.com on 07/31/07 05:45:32.
updated -- it asumes your Site domain is just the base (e.g. mysite.com)

Change History

07/30/07 22:43:47 changed by tzellman@gmail.com

  • attachment middleware.py added.

SubdomainURLsMiddleware

07/30/07 22:46:13 changed by anonymous

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Example of what you'd change in settings.py:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'mysite.myapp.middleware.SubdomainURLsMiddleware'
)

DOMAINS_URLCONF = {
    'mobile' : 'mysite.mobile_urls',
    'companyA : 'mysite.companyA_urls',
}

07/30/07 23:13:55 changed by Max Battcher <me@worldmaker.net>

I appreciate this as a workable solution in some cases, but I don't think it is general enough. This doesn't easily address cases where many/all of the subdomains are sharing a single URLCONF, particularly a deeply nested URLCONF, and yet still need to differentiate content. It also doesn't provide anything in the way of support for standard contrib apps like Flatpages, as there would be no way to have different flatpages appearing on different subdomains solely by modifying the URLCONF.

07/30/07 23:25:59 changed by tzellman@gmail.com

Yeah, I agree with you Max. For domains that have at least some overlapping url/view mappings, this solution would require duplication, which is not ideal. This solution satisfies only a small percentage of subdomain needs out there, thus making it not generic enough. However, I think such a "recipe", for lack of a better term, could be useful to some. I guess I'm not actually expecting this to become part of the Django API, but rather wanted to get the discussion started up again, to see if we could draft up a generic solution. I would be more than willing to be a part of such an effort.

07/31/07 05:45:32 changed by tzellman@gmail.com

  • attachment middleware.2.py added.

updated -- it asumes your Site domain is just the base (e.g. mysite.com)

08/03/07 14:50:37 changed by Herbert Poul <herbert.poul@gmail.com>

fyi - i have done something similar for my project http://sct.sphene.net/ but the middleware would also support regular expressions to define urlconfs .. a config would look like:

SPH_HOST_MIDDLEWARE_URLCONF_MAP = {
  r'^(P<groupName>\w+)\.sphene\.net': { 'urlconf': 'sphurlconfs.community_urls', }
}

all named arguments of the regular expression are then put into a thread local so it can be used by views to further distinguish between the domains. you can see the implementation at http://yourhell.com/svn/root/django/communitytools/trunk/sphenecoll/sphene/community/middleware.py ('MultiHostMiddleware')

(i'm using 'Groups' which are similar to the concept of the 'Sites' object in django as they are used to build different sites with different configuration for my wiki and forum apps) - if something like this would be put into django the flatpages application could be easily rewritten to use a thread local to retrieve the current Site object instead of using the settings.

10/02/07 14:05:06 changed by anonymous

  • cc set to drackett@mac.com.

(in reply to: ↑ description ) 10/16/07 21:50:50 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

Replying to tzellman@gmail.com:

I noticed issue #4438, and it wasn't exactly what I wanted, or the right solution, so I started researching.

Note that I've put a new ticket up in #4438 which handles the issue a lot better.

12/02/07 14:51:57 changed by jacob

  • status changed from new to closed.
  • resolution set to wontfix.

A bit too special-purpose for inclusion in Django, but a good candidate for djangosnippets.org.


Add/Change #5022 (Proposed middleware: SubdomainURLsMiddleware)




Change Properties
Action