Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Django Debugging – Add a Debug Bar to your Django Webpage

Django Debugging

In this article, we talk about how we can perform Django debugging. We will learn an important technique required in web development, known as debugging, and then further move on to learn how to add the debugger tool to our websites.

So let’s get started !!

The Art of Debugging

No matter how professional, every developer deals with errors at part of their life. Also, debugging errors is not an easy task; it takes up a lot of time to first point out the error and eliminate it. Hence every developer must know debugging.

But with Django, guess what, the debugging process has been made so much simpler for you. You just need to install the Django debugger tool on your web application, and that’s it you are done. 

Hence now that we know why Debugging is essential, let’s get our hands dirty in it.

Prerequisites for Debugging in Django

Now to use the debug toolbar, we need a website. If you have your project, you can use it. Otherwise, add the following View and the corresponding URL path.

def SampleView(request):
    Html = '<body><h1>Django Caching<h1><br><p>Welcome to Caching Tutorial</p></body>'
    return HttpResponse(html)

Ensure that the <body> is present; otherwise, the debugger tool won’t get displayed on the webpages, whose templates do not have the <body> tag.

The URL path for the code will be:

path('sample/', SampleView),

Now for the next section, you can store the cache in any of the form shown above:

The Django Debugger Tool

The debugger tool consists of various debugging options, which we can use on the particular webpage. We will now look into the various tools provided by the tool bar

  • Version: Gives the Version of Django we are using.
  • Time: Tells the time taken to load the web page
  • Setting: Shows the settings of the web page
  • Request: Shows all the elements which were requested – the views, cookies, etc.
  • SQL: Shows the list of the SQL DB calls
  • Static Files: Gives information about static files
  • Templates: Gives information about Templates
  • Cache: Tells the information about the caches present
  • Logging: Shows the number of Logs registered. (See Django Logging for information about Logs)

Installing the Django Debugging tool

In this section, we will install all the requirements needed for the Django Debugger tool bar

1) Install Django Debugger toolbar

To install the Django debugger toolbar we use the pip install command. Run the following code in the terminal/shell:

pip install django-debug-toolbar

2) Add the debugger tool in INSTALLED_APPS

In settings.py, add the following line in the INSTALLED_APPS section

INSTALLED_APPS = [
    # ...
    'debug_toolbar',
]

Also ensure that the following line is present in the settings.py file

STATIC_URL = '/static/'

The above code should be present already, but if it is not there then just add the above code at the last.

3) Import Debug Toolbar in to urls.py

To use the debug toolbar on the webpage, we must link to the URL paths of the webpages.

Hence in urls.py, add the code:

from django.conf import settings
from django.URLS import path,include

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

Make sure DEBUG is set to TRUE, in settings.py for this to work.

Here we are adding the debug_toolbar to the URL patterns list, where all the other URLs are present

Debug ToolBar urls.py
Debug ToolBar urls.py

4) Enabling MiddleWare

The middleware associated with the Django Debug toolbar has to be added into the MIDDLEWARE section in settings.py

MIDDLEWARE = [
    # ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

5) Mentioning INTERNAL_IPS

The debug toolbar is shown only if the IP is present in the INTERNAL_IPS list in settings.py. For local development purposes, add the IP ‘127.0.0.1’ into the list.

INTERNAL_IPS = [
    '127.0.0.1',
]

If the INTERNAL_IPS list is not already present, add the above list at the last of settings.py and add the IP ‘127.0.0.1.’

Output of the Django Debugging Tool

With all the code added, visit 127.0.0.1:8000/sample/ in your preferred browser.

If you see the below output, your implementation is successful! If not, check if any of the pieces of code above are missing from your files.

Debug toolbar
Debug toolbar

That’s it the tool bar will appear on the right side of the webpage.

Conclusion

That’s it, guys!! This was all about the Django debugger too. Do check out the Django Exceptions Handling article to know more about debugging exceptions.

See you in the next article!! Till then, keep practicing !!