Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django {{site}} template context not working?

This should be a super simple one. I'm pretty sure that I've used this context successfully in the past in my templates for linking purposes. My belief was that this was built into the RequestContext instance somehow or other.

the SITE_ID record in my settings file is correct. I've included a RequestContext instance for all my views and i have included the contrib.auth app, which may be relevant in this case.

Is the {{site}} context somehow built in or should I query the Sites object for the instance?

thanks all, Brendan

like image 258
Ben Avatar asked Feb 23 '11 17:02

Ben


People also ask

What is a template in Django?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.

How do I find the parent template in Django?

If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template. See Template inheritance for more information. Normally the template name is relative to the template loader’s root directory.

How do I override a template in Django?

In order to override one or more of them, first create an admin directory in your project’s templates directory. This can be any of the directories you specified in the DIRS option of the DjangoTemplates backend in the TEMPLATES setting.

How to generate HTML dynamically in Django?

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted. For a hands-on example of creating HTML pages with templates, see Tutorial 3.


Video Answer


2 Answers

Django strives to be explicit, so it is unlikely that it would set any context by it self. There has to be context processor which sets {{site}} in settings.CONTEXT_PROCESSORS. I've checked django.core.context_processors and django.contrib.sites and there is no such processor which sets site. So you probably had a third-party context processor which does that.

It is very easy to write context processor:

myproject/context_processors.py:

    from django.contrib.sites.models import Site

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

myproject/settings.py:

    CONTEXT_PROCESSORS += ['myproject.context_processors.site']
like image 140
Ski Avatar answered Oct 15 '22 19:10

Ski


It wont hurt to create a custom context processor

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

Note that get_current() uses SITE_ID, which is a global setting in the project (defined in settings.py). If you are going to support multi-sites, you need the SITE_ID variable to change its value depending on the current site being accessed.

Here is a nice snippet that will make it work.

like image 37
gladysbixly Avatar answered Oct 15 '22 20:10

gladysbixly