如何在Django的模板中显示网站的用户友好名称? [英] How to display the site's user friendly name in a template in Django?

查看:83
本文介绍了如何在Django的模板中显示网站的用户友好名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在模板中显示Django-CMS站点名称的方法。

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

我在本指南。默认站点名为Example.com,我可以在管理部分中将此名称更改为我的超级站点。
接下来,我尝试自定义主题并将主题应用到本指南。建议使用 {{request.site.name}} 在页面上显示站点名称。但这变成空了, {{request.site}} 也是如此。

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 }}.

我进一步搜索并发现了几个以越来越复杂的命题接近主题的页面越老。最近的类似这个的建议是与django-cms无关,而与django本身和站点框架有关。

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.

我查看了文档,尤其是此页面,如果我正确理解,则表示该站点中间件一旦激活,便在请求中包含一个站点对象模板中可用的对象。我检查并启用了网站框架(在INSTALLED_APPS设置中定义了 django.contrib.sites,定义了SITE_ID并运行了迁移)。因此,我不明白为什么 {{request.site}} 为空。

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.

为了弄清楚事情,我不是在寻找域名或主机。我正在寻找用户友好的名称,该名称在django-cms的管理/站点更改站点部分的显示名称下找到。

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.

谢谢

推荐答案

请确保添加'django.core.context_processors.request'到上下文处理器。

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

像这样:

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

还将 django.contrib.sites.middleware.CurrentSiteMiddleware 添加到您的 MIDDLEWARE 设置中。

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

这篇关于如何在Django的模板中显示网站的用户友好名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆