Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the current Site accessible from a template?

I'm trying to simply get the current Site from within a template for parsing like so:

<h3>{{ site.name }}</h3>

Unfortunately, this isn't bringing anything up.

Is there a way to get access to the current site from a template?

like image 693
Naftuli Kay Avatar asked Sep 19 '11 05:09

Naftuli Kay


1 Answers

The title of your question presumes that "view" and "template" are interchangeable -- they're not. In order to get the current site in a template, it needs to be added to the context that is used to render the template. If you're using a RequestContext, you can write a context processor to do this automatically.

You can write a context processor to do this like so:

from django.contrib.sites.models import Site

def site_processor(request):
    return { 'site': Site.objects.get_current() }

Then, add it to your TEMPLATE_CONTEXT_PROCESSORS, and use it like so:

<h3>{{ site.name }}</h3>
like image 140
bradley.ayers Avatar answered Nov 04 '22 10:11

bradley.ayers