7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

django-debug-toolbarを設定する

Last updated at Posted at 2019-04-30

概要

こんにちは、すずです。
今回はrunserverを起動しながらエラーを迅速に対処するためのツールをご紹介いたします!(^^)!

それでは実際に使っていきましょう!

環境

Pytho3.7.2
Django2.1.7
django-debug-toolbar1.11

やり方

まずはdjango-debug-toolbarをインストールしましょう↓↓
$ pip install django-debug-toolbar

これで自身の環境にdjango-debug-toolbarを入れることができました!
次はsettings.pyにアプリ使用の設定をしましょう(^^)/

settings.py

# mysite/mysite/settings.py
# settings.pyの一番下に付け足しましょう

・・・・
・・・・・・・

if DEBUG:
    def show_toolbar(request):
        return True


    INSTALLED_APPS += (
        'debug_toolbar',
    )
    MIDDLEWARE += (
        'debug_toolbar.middleware.DebugToolbarMiddleware',
    )
    # ここで表示する内容を設定できます↓↓基本的にはこれでok
    DEBUG_TOOLBAR_CONFIG = {
        'SHOW_TOOLBAR_CALLBACK': show_toolbar,
    }

if文を使ってDEBUGがTrueだった場合、設定を後付けするようにしております。
次にurls.pyの設定をしていきます

urls.py

# mysite/mysite/urls.py
# urls.pyの一番下に付け足しましょう

・・・・・
・・・・・・・

if settings.DEBUG:
    import debug_toolbar

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

まとめ

今回はデバックツールの紹介でしたが、
このdjango-debug-toolbarはDjango使う上で必須のツールだと思います。

また実用編としては前に記事にもさせていただいた

Djangoでsettings.pyを上書きする方法

と併用して使用することをお勧めします!
プロジェクトごとに何度も書くのはめんどくさいですからねw

この記事の詳しい内容はブログに書いてありますので見てみてください
またTwitterではこんな感じなお役立ち情報を流してますので是非覗いてみてください↓↓

webアプリが世に出ていくまでのブログ
Twitter すず(@S2notC2)

参考資料

django-debug-toolbarを設定する

7
8
0

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
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?