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?
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>
direct_to_template
uses RequestContext
, however you'll need to write your own context processor as there isn't one in Django. Alternatively you can pass an argument to direct_to_template
that adds the current site to the context. –
Marlin site_processor
instead of site
? –
Deflective dict
, and the keys in the dict will be made available as variables when rendering the context. Thus you could change the site
key to something else, or add additional key/value pairs to the dictionary and access them as well in the template. –
Milburt settings.py
file. –
Flamen Weirdly, using the bradleyayers processor gave Null results, so instead of using the Site framework, I used the parameter inside the request.
So the processor will look like that :
def host_processor(request):
return { 'host': request.get_host() }
Hope it helped
© 2022 - 2024 — McMap. All rights reserved.
django.views.generic.simple.direct_to_template
, so I should be covered, right? There isn't a built-in context processor for this? – Emanative