Django cross-site reverse URLs

前端 未结 4 491
天命终不由人
天命终不由人 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:05

    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

提交回复
热议问题