1. Introduction

Hello, I’m Anguo!

When using Django for Web development, it’s important to install some third-party applications to improve development efficiency

This article will recommend two very useful apps

  • django_extensions

  • debug_toolbar

2. django_extensions

Django_extensions is a repository that collects global custom administrative extensions for Django projects

First, install the dependencies in the target virtual environment

Pip3 install Django-extensionsCopy the code

Then, add the App in the project’s configuration file settings.py

DEBUG = True # add App: django_extensions INSTALLED_APPS = (... 'django_extensions,...).Copy the code

You can then use it for project debugging

The common functions are as follows:

2-1 Enhanced Shell

As we all know, the original project was put into debug mode using the “./manage.py shell “command, and all Model operations had to be imported manually

However, by adding the Django_Extensions application, you can use the “./manage.py shell_plus “command to access the enhanced Shell and automatically import all models under the project, making it easy to debug

2-2 Checking template errors

You can use the./manage.py validate_templates command to detect errors in templates

Localhost :xh_tools xingag$./manage.py validate_templates 0 errors foundCopy the code

2-3 View the route list

Use the “./manage.py show_urls “command to view the mapping tables of all routes and view functions for your project

More functions can be referred to:

Github.com/django-exte…

3. django_toolbar

Django_toolbar is a Django development tool that displays the overall project information, request response, SQL, cache, and more in real time in Debug mode

First, install the dependency packages in a virtual environment

Pip3 install Django-debug-toolbarCopy the code

Then configure django_toolbar in the project configuration file settings.py

3-1 Adding an application

DEBUG = True # add App: django_extensions INSTALLED_APPS = (... 'debug_toolbar,...).Copy the code

3-2 Enable django_toolbar middleware

Start the Django_Toolbar MIDDLEWARE as early as possible in the MIDDLEWARE list

Note: The location of the middleware is important; the Djano_Toolbar middleware must be behind any other middleware that encodes the response content

​# settings.py

MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Copy the code

Reference:

Docs.djangoproject.com/en/dev/ref/…

3-3 Configure the internal IP address

You must set INTERNAL_IPS to the local IP address in configuration file settings.py to display the debugging toolbar on the web page

# settings.py INTERNAL_IPS = ['127.0.0.1', 'localhost']Copy the code

3-4 (Optional) Configuring display Content

The default toolbar displays everything, and you can also define the value of the DEBUG_TOOLBAR_PANELS list to specify a portion of the content to display

For example, configure only load time, request headers and response headers, request information, SQL statements, cache, and log information

# settings.py

DEBUG_TOOLBAR_PANELS = [
    'debug_toolbar.panels.timer.TimerPanel',
    'debug_toolbar.panels.headers.HeadersPanel',
    'debug_toolbar.panels.request.RequestPanel',
    'debug_toolbar.panels.sql.SQLPanel',
    'debug_toolbar.panels.cache.CachePanel',
    'debug_toolbar.panels.logging.LoggingPanel',
]
Copy the code

Finally, configure the URL for the debug toolbar in the urls.py file under the project

Urlpatterns = [path('', include('index.urls'))), path('admin/', admin.site.urls), *)$', static.serve, {'document_root': Settings. STATIC_ROOT}, name='static')] import debug_toolbar urlpatterns = [ path('__debug__/', include(debug_toolbar.urls)), ] + urlpatternsCopy the code

After running the project in debug mode, you can see live toolbar information on a Web page

More functions can be referred to:

Github.com/jazzband/dj…

4. The last

This article recommends two useful Django development AIDS that can help you debug your project and quickly locate problems

If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!