Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add django-template-isdebug #13

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions public/django-template-isdebug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: Djangoのテンプレートでデバッグモードを判定する
tags:
- "Programming"
- "Python"
- "django"
private: false
updated_at: ""
id: null
organization_url_name: null
slide: false
ignorePublish: false
---

Djangoのテンプレートすべてのページから、`{% if DEBUG %}` を用いてデバッグモードを判定できるようにする方法を紹介します。`context_processors` を用いることで行います。

※ これは2019/7/23に個人ブログで公開した記事を移植したものです。

## ビューでデバッグモードを判定する方法

Djangoのビューでデバッグモードを判定する方法として下記があります。

```py
from django.conf import settings
DEBUG = settings.DEBUG
print(DEBUG) # True
```

いちいちこの変数をテンプレートに渡すのは面倒なので、`context_processors` を用いて `DEBUG` 変数を定義してみます。

## `django.template.context_processors.debug` は微妙

似たような機能として、標準でも `django.template.context_processors.debug` が存在します。

https://docs.djangoproject.com/ja/2.2/ref/templates/api/#django.template.context_processors.debug

しかし、この機能は `INTERNAL_IPS` を適切に設定していないと機能しません。

https://docs.djangoproject.com/ja/2.2/ref/settings/#internal-ips

Dockerなどを用いると、ローカル開発環境でさえも様々なIPからアクセスされるのでなかなか使いにくいです。

## context_processors を用いた DEBUG 変数定義

そこで、`INTERNAL_IPS` を気にせずsettings.pyのDEBUGを問答無用でテンプレートから呼び出せるcontext_processorsを作ってみます。

app/context_processors.pyに次のプログラムを追加します。

```py
from django.conf import settings

def is_debug(request):
return {"DEBUG": settings.DEBUG}
```

そして、setting.pyの `TEMPLATES['OPTIONS']['context_processors']` に今回追加したcontext_processorsを追加します。

```py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug', # ←消して良い
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'app.context_processors.is_debug', # 追加
],
},
},
]
```

以上で、`{% if DEBUG %}` が利用できます。