Home  >  Article  >  Backend Development  >  Django debugging tool django-debug-toolbar installation and usage tutorial

Django debugging tool django-debug-toolbar installation and usage tutorial

高洛峰
高洛峰Original
2016-10-17 13:46:531541browse

In website development, it is inevitable to debug the page. When using django to develop the site, you can use django-debug-toolbar for debugging. Installing this plug-in is very useful. I initially wanted to view all the context variable values ​​​​in a certain page. Of course, you can also see various information such as HTTP headers, templates, caches, etc. In short, it is very comprehensive and easy to use.

In the past, I was more accustomed to installing pycharm in Windows for development. The project was deployed in a virtual machine and the effect was viewed in a local browser. This method would be a bit troublesome in debugging. The emergence of django-debug-toolbar solved this problem.

Here's how to install and use django-debug-toolbar:

1. Install

Use the command

sudo pip install django-debug-toolbar

Install django-debug-toolbar. (Note that Django version and debug_toolbar version are compatible. If you don’t have pip, please install it first. See tutorial: Detailed explanation of python package manager pip installation)

2. 配置

Add 'debug_toolbar.middleware.DebugToolbarMiddleware' to the project in settings.py within MIDDLEWARE_CLASSES.

Add INTERNAL_IPS = ('127.0.0.1',) in settings.py, (from which ip to access the site, show debug_toolbar)

Add 'debug_toolbar' in INSTALLED_APPS

Make sure the DEBUG option is true

Add DEBUG_TOOLBAR_PANELS option

Finally set the template and add the template directory of debug_toolbar to TEMPLATE_DIRS.

The code is as follows:

DEBUG_TOOLBAR_PANELS = [
    'debug_toolbar.panels.versions.VersionsPanel',
    'debug_toolbar.panels.timer.TimerPanel',
    'debug_toolbar.panels.settings.SettingsPanel',
    'debug_toolbar.panels.headers.HeadersPanel',
    'debug_toolbar.panels.request.RequestPanel',
    'debug_toolbar.panels.sql.SQLPanel',
    'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    'debug_toolbar.panels.templates.TemplatesPanel',
    'debug_toolbar.panels.cache.CachePanel',
    'debug_toolbar.panels.signals.SignalsPanel',
    'debug_toolbar.panels.logging.LoggingPanel',
    'debug_toolbar.panels.redirects.RedirectsPanel',
]

Okay, you are done here. Note that if you create a new site for testing debug_tool, be sure to render a template so that the site has an accessible page, otherwise you will not get the debug_tool interface.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn