在Django1.9中更改站点名称

2024-05-23 14:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在遵循一个教程,其中介绍了一个自定义的bootstrap3模板,并使用它构建了一个Django站点。在本教程中,他们建议更改以下模板片段:

<a class="navbar-brand" href="index.html">Start Bootstrap</a>

到下面的片段。在

^{pr2}$

但是,当我进行此更改时,没有显示站点名称。我想知道我应该在哪里设置这个名字。如果有帮助的话,我使用的是Django CMS,在管理的Sites部分中只有一个名为example.com的站点。在


Tags: django模板index站点html教程bootstrapstart
3条回答

您可以在管理的Sites部分添加站点名称或更改名为example.com的现有站点。在

要访问Site对象,需要在settings.py中提供{}

如果要更改example.com,请在“设置”中使用SITE_ID=1

要在django模板中呈现站点名称,请使用

 from django.contrib.sites.models import Site
 current_domain = Site.objects.get_current().domain

然后将current_domain传递给模板

似乎您必须启用站点框架,请参阅doc。在

有同样的问题,找到了this in the documentation。在

If you often use this pattern:

from django.contrib.sites.models import Site

def my_view(request):
    site = Site.objects.get_current()

... there is simple way to avoid repetitions. Add django.contrib.sites.middleware.CurrentSiteMiddleware to MIDDLEWARE_CLASSES. The middleware sets the site attribute on every request object, so you can use request.site to get the current site.

相关问题 更多 >