How to get site name in django template?

最后都变了- 提交于 2019-12-24 03:59:50

问题


In the admin interface, I can see how I can specify the name of the site. But I can't find the documentation for how to use it in my templates.


回答1:


You can set site name in class requests.RequestSite. It is a class that shares the primary interface of Site (i.e., it has a domain and name attributes) but gets its data from a Django HttpRequest object rather than from a database. You have to use __init__(request) in it to set the name and domain attributes to the value of get_host().

In syndication framework, templates for title and description automatically have access to a variable {{ site }}, which is the Site object representing the current site. Also, the hook for providing item URLs will use the domain from the current Site object if you don’t specify a fully-qualified domain.

In authentication framework, django.contrib.auth.views.LoginView passes the current Site name to the template as {{ site_name }}.

Reference




回答2:


I faced a similar problem and found the solution answered in this post: How to display the site's user friendly name in a template in Django?

Basically, in my settings.py, I had to add 'django.core.context_processors.request', to my TEMPLATES -> OPTTIONS -> context_processors. Then you can access the site name in the template with {{ request.site.name }}.



来源:https://stackoverflow.com/questions/21196940/how-to-get-site-name-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!