django-debug-toolbar的安装

1. 介绍

Django Debug Toolbar是Django开发中必备利器,可以帮助开发者快速了解项目的整体信息以及每个页面包括sql信息,http相关信息。

django版本必须大于等于2.2才可以使用

2. 安装

pip3 install django-debug-toolbar -i https://pypi.douban.com/simple

3. 配置

3.1 settings.py 文件

# 1.app
INSTALLED_APPS = [

    'debug_toolbar'

]

# 2.中间价
MIDDLEWARE = [
    # ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    # ...
]

# 3.设置本地IP, debug_toolbar只能在localhost本地测试环境下运行。
INTERNAL_IPS = [
    # ...
    '127.0.0.1',
    # ...
]

3.2 urls.py

# urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from blog import views
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('article_list/', views.article_list),

]
if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
                      path('__debug__/', include(debug_toolbar.urls)),
                  ] + urlpatterns

4.案例

4.1 url.py

# urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from blog import views
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('article_list/', views.article_list),

]
if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
                      path('__debug__/', include(debug_toolbar.urls)),
                  ] + urlpatterns

4.2 view.py

from django.shortcuts import render

from blog import models


def article_list(request):
    if request.method == 'GET':
        # select_related---->queryset
        article_queryset = models.Article.objects.all()
        return render(request, 't2.html', context={'article_queryset': article_queryset})

 4.3 图解

django-debug-toolbar的安装_第1张图片

 

你可能感兴趣的:(django,python,后端)