Django cross-site reverse URLs

前端 未结 4 486
天命终不由人
天命终不由人 2021-02-01 10:41

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

4条回答
  •  有刺的猬
    2021-02-01 11:01

    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'),
    )
    

提交回复
热议问题