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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With