Help us understand the problem. What is going on with this article?

django-debug-toolbarの導入

環境

  • 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の中身を表示させたら確認完了。

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした