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_URLCONF
s. 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_ID
s 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.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'),
)
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)
Reverse overwrite for Django 1.7.x using the same settings from @drdaeman
# -*- coding: utf-8 -*-
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, current_app=None):
try:
return __real_reverse(viewname, urlconf, args, kwargs, prefix, current_app)
except urlresolvers.NoReverseMatch, no_match:
external_urlconfs = getattr(settings, 'EXTERNAL_URLCONFS', [])
for p, c in external_urlconfs:
urlconf = c
try:
return p + __real_reverse(viewname, urlconf, args, kwargs, prefix, current_app)
except urlresolvers.NoReverseMatch:
pass
raise no_match
urlresolvers.reverse = reverse
I placed the code in the urls.py file to execute at startup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With