Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the site's user friendly name in a template in Django?

I am searching for a way to display my Django-CMS site name in a template.

I created a basic Django-CMS site following this guide. The default site is named Example.com and I was able to change this name, from within the admin section, to "My Super Site". Following what, I tried to customize and apply a theme to it, following this guide. It suggests to use {{ request.site.name }} to display the name of the site on a page. However this turns up empty and so does {{ request.site }}.

I searched further and found several pages approaching the subject with increasingly complicated propositions the older they were. Recent ones, like this one, suggested that this was not related to django-cms but to django itself and the sites framework.

I looked into the documentation, particularly this page and this one which, if I understood properly, indicates that the site middleware, once activated, includes a site object in the request object available in the templates. I checked and the sites framework is enabled ('django.contrib.sites' in INSTALLED_APPS setting, SITE_ID defined and migrate ran). So, I don't understand why {{ request.site }} is empty.

To clarify things, I am not looking for the domain name or the host. I am looking for the user friendly name, the one found in django-cms under 'Display Name' in the 'Change site' section of administration/sites.

Thank you for your help.

like image 794
Xosted Avatar asked Mar 25 '17 21:03

Xosted


1 Answers

make sure to add 'django.core.context_processors.request' to your context processors.

Like so:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'django.core.context_processors.request',
            ],
        },
    },
]

Also add django.contrib.sites.middleware.CurrentSiteMiddleware to your MIDDLEWARE settings.

like image 59
Paulo Avatar answered Sep 28 '22 05:09

Paulo