LoginSignup
2

More than 3 years have passed since last update.

django-debug-toolbarの導入

Last updated at Posted at 2019-09-25

環境

  • python 3.7.4
  • django 2.2.5
  • django-debug-toolbar 2.0

インストール

pip install django-debug-toolbar

setting.pyの変更


INSTALLED_APPS = [
  ・・・
  'django.contrib.staticfiles', # デフォルトで設定されているはず。
  ・・・
    'debug_toolbar', # 追加する。
  ・・・
]
MIDDLEWARE = [
  ・・・
    'debug_toolbar.middleware.DebugToolbarMiddleware', # 追加する
]

REMOTE_ADDRの確認方法については後述する。

# ローカル開発環境の場合
INTERNAL_IPS = ['127.0.0.1', REMOTE_ADDR] # 追加する

urls.pyの変更


# importしていないものがあれば追加する。
from django.conf.urls import include
from django.urls import path
from django.conf import settings


urlpatterns=[
  ・・・
]

# 以下を追加
if settings.DEBUG:
    import debug_toolbar

    urlpatterns = [
                      path('__debug__/', include(debug_toolbar.urls)),
                  ] + urlpatterns

REMOTE_ADDRの確認方法

適当なviews.pyの関数に以下を追加。

def confirm(request):

    # contextの中にrequest.META['REMOTE_ADDR']を追加する。
    context = {
      'remote_addr': request.META['REMOTE_ADDR']
    }
    return render(request,'path/to/template_file.html,context=context)

あとはrenderで指定したhtmlファイルのbodyタグの中に

{{ remote_addr }}

と書いてrunserverでhtmlの中身を表示させたら確認完了。

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2