[Answered ]-Getting Site_ID in template Django

1👍

You can create a context processor to automatically add site_id to the context: http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

But I would opt for a different solution. You can simply add an extra template directory per site so Django will try the templates specifically for that site first and fall back to the normal templates if they’re not available.

👤Wolph

1👍

To extend the idea of WoLph with the context processor, I would maybe even add the switching of the template to the context processor which would clean up your templates, as otherwise you may have to repeat the if clause quite often:

from django.contrib.sites.models import Site

def base_template(request):
    site = Site.objects.get_current()
    template = "base%s.html" % str(site.pk)
    return {'BASE_TEMPLATE': template}

And in your template: {% include BASE_TEMPLATE %}

Looks nicer to me than the switching in the templates!

0👍

Another solution would be writing a Middleware to set ´request.site´ the current site id.

Leave a comment