views:

555

answers:

3

Probably simple question and I'm just missing something, but I'm stuck out of ideas.

I have Django project serving several sites with distinct sessions.py and completely different ROOT_URLCONFs. One site handles user registration, authentication and profile settings, other site (on another domain) acts as file manager and so on. Sites are sharing the same DB, media and templates. All sites are sharing the same userbase, implementing sort of transparent single-sign-on/single-sign-off mechanism. It is just like one big site, spanning across several domains.

The problem is, I have a lot of {% url %} tags in my templates, and they don't work when template's used on other sites. And I'd like to avoid hardcoding URLs as much as possible.

For example, on site A (a.example.org) I have an

url('^users/$', 'example.accounts.list_users', name='list_users'),

entry in A's URLconf. Then, in some global_menu.html template I have {% url list_users %} and obviously it works perfectly, resulting in "/users/".

Now, there's site B (b.example.org), sharing a lot of internals with A. To have common look-and-feel I want to use the same global_menu.html on site B and want {% url list_users %} to output "http://a.example.org/users/". What's the best way I can achieve this?

Currently, I'm using separate global_menu.html for each site, but this violates DRY principle, and not really convenient. And, yes, I'm using Django's contrib.sites framework with distinct SITE_IDs defined in settings.py for each site, but not yet actually using it anywhere else.

Update: Currently I'm thinking of reimplementing url tag or monkey-patching reverse(), to call the original one, and on exceptions perform additional look up in some "foreign URI list". If there already exists anything like this — I'd be happy to hear.

Thank you in advance for answers!

A: 

I would suggest making two changes. (1) Move templates to a common directory (rather than per-application) if you already haven't. (2) Investigate the newly added URL namespaces feature.

The first change will allow you to have a common base template and selectively override it for various apps/sites. The second might serve to make your URLs "drier".

Manoj Govindan
(1) is already done. (2) is a fancy feature, thanks for pointing out, but doesn't help much for non-existent named URLs. I've tried to add something like `url('http:\/\/a\.example\.com/users/', lambda r: None, name='list_users')` to B's URLconf, but it doesn't work as I'd expect (returns "/http:...").
drdaeman
+1  A: 

Yes, you'd need to make your own {% url %} tag which uses it's own reversal method.

For example, to reverse specifically against the site_a urlconf then you could use a method like this:

from django.core.urlresolvers import reverse
import site_a

def site_a_reverse(viewname, args=None, kwargs=None):
    # If your sites share the same database, you could get prefix from Site.objects.get(pk=site_a.settings.SITE_ID)
    prefix = 'http://a.example.com/'  # Note, you need the trailing slash
    reverse(viewname, urlconf=site_a.urls, args=args, kwargs=kwargs, prefix=prefix)
SmileyChris
Thank you! I've ended up monkey-patching `reverse()` (put my own code in an separate answer).
drdaeman
A: 

I've implemented it by overriding django.core.urlresolvers.reverse with my custom function:

from django.core import urlresolvers
from django.conf import settings

__real_reverse = urlresolvers.reverse

def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None):
    try:
        return __real_reverse(viewname, urlconf, args, kwargs, prefix)
    except urlresolvers.NoReverseMatch, no_match:
        external_urlconfs = getattr(settings, 'EXTERNAL_URLCONFS', [])
        for p, c in external_urlconfs:
            c = urlresolvers.RegexURLResolver(r'^/', c)
            try:
                return p + c.reverse(viewname, *args, **kwargs)
            except urlresolvers.NoReverseMatch:
                pass
        raise no_match

urlresolvers.reverse = reverse

Then listing URLconfs in settings.py like this:

ROOT_URLCONF = 'project.urls_a'

EXTERNAL_URLCONFS = (
    ('http://b.example.com/', 'project.urls_b'),
)
drdaeman