Django cross-site reverse URLs

前端 未结 4 593
天命终不由人
天命终不由人 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条回答
  •  萌比男神i
    2021-02-01 10:58

    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)
    

提交回复
热议问题