# https://docs.djangoproject.com/en/3.0/ref/request-response/#attributes-set-by-middleware
1. template에서 site객체 참조하고 싶다면
settings.py
-------------------------------------------------------------------------------
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.sites.middleware.CurrentSiteMiddleware', <--- 추가
]
template에서 아래와 같이 사용가능
{{ request.site.name }}
# https://docs.djangoproject.com/en/3.0/ref/templates/api/#using-requestcontext
2. template에서 user 객체처럼 request객체 통하지 않고 바로 사용하고 싶다면....
\Lib\site-packages\django\template\context_processors.py
-------------------------------------------------------------------------------
....
def request(request):
return {'request': request}
# 추가
def site(request):
return {'site': request.site}
settings.py
-------------------------------------------------------------------------------
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.site', <-- 추가
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
template에서 아래와 같이 사용가능
{{ site.name }}