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!
I’ve implemented it by overriding
django.core.urlresolvers.reversewith my custom function:Then listing URLconfs in
settings.pylike this: