问题
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