{"id":8250,"date":"2020-08-29T19:33:46","date_gmt":"2020-08-29T19:33:46","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8250"},"modified":"2020-08-29T19:33:48","modified_gmt":"2020-08-29T19:33:48","slug":"django-debugging","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/django\/django-debugging","title":{"rendered":"Django Debugging &#8211; Add a Debug Bar to your Django Webpage"},"content":{"rendered":"\n<p>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. <\/p>\n\n\n\n<p>So let\u2019s get started !!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Art of Debugging<\/strong><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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\u2019s it you are done.\u00a0<\/p>\n\n\n\n<p>Hence now that we know why Debugging is essential, let\u2019s get our hands dirty in it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites for Debugging in Django<\/h2>\n\n\n\n<p>Now to use the debug toolbar, we need a website. If you have your <a href=\"https:\/\/www.askpython.com\/django\/django-app-structure-project-structure\" class=\"rank-math-link\">project<\/a>, you can use it. Otherwise, add the following <a href=\"https:\/\/www.askpython.com\/django\/django-views\" class=\"rank-math-link\">View<\/a> and the corresponding URL path. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef SampleView(request):\n    Html = &#039;&lt;body&gt;&lt;h1&gt;Django Caching&lt;h1&gt;&lt;br&gt;&lt;p&gt;Welcome to Caching Tutorial&lt;\/p&gt;&lt;\/body&gt;&#039;\n    return HttpResponse(html)\n<\/pre><\/div>\n\n\n<p>Ensure that the <strong>&lt;body&gt;<\/strong> is present; otherwise, the debugger tool won&#8217;t get displayed on the webpages, whose templates do not have the <strong>&lt;body&gt; tag.<\/strong><\/p>\n\n\n\n<p>The <a href=\"https:\/\/www.askpython.com\/django\/django-url-mapping\" class=\"rank-math-link\">URL path<\/a> for the code will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npath(&#039;sample\/&#039;, SampleView),\n<\/pre><\/div>\n\n\n<p>Now for the next section, you can store the cache in any of the form shown above:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Django Debugger Tool<\/h2>\n\n\n\n<p>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<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Version:<\/strong> Gives the Version of Django we are using.<\/li><li><strong>Time<\/strong>: Tells the time taken to load the web page<\/li><li><strong>Setting:<\/strong> Shows the settings of the web page<\/li><li><strong>Request:<\/strong> Shows all the elements which were requested &#8211; the views, cookies, etc.<\/li><li><strong>SQL:<\/strong> Shows the list of the SQL DB calls<\/li><li><strong>Static Files:<\/strong> Gives information about static files<\/li><li><strong>Templates:<\/strong> Gives information about Templates<\/li><li><strong>Cache:<\/strong> Tells the information about the caches present<\/li><li><strong>Logging:<\/strong> Shows the number of Logs registered. (See <a href=\"https:\/\/www.askpython.com\/django\/django-logging\" class=\"rank-math-link\">Django Logging<\/a> for information about Logs)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Installing the Django Debugging tool<\/strong><\/h2>\n\n\n\n<p>In this section, we will install all the requirements needed for the Django Debugger tool bar<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Install Django Debugger toolbar<\/h3>\n\n\n\n<p>To install the Django debugger toolbar we use the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" class=\"rank-math-link\">pip install<\/a> command. Run the following code in the terminal\/shell:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\npip install django-debug-toolbar\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2) Add the debugger tool in INSTALLED_APPS<\/h3>\n\n\n\n<p>In settings.py, add the following line in the <strong>INSTALLED_APPS<\/strong> section<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nINSTALLED_APPS = &#x5B;\n    # ...\n    &#039;debug_toolbar&#039;,\n]\n<\/pre><\/div>\n\n\n<p>Also ensure that the following line is present in the settings.py file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSTATIC_URL = &#039;\/static\/&#039;\n<\/pre><\/div>\n\n\n<p>The above code should be present already, but if it is not there then just add the above code at the last.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Import Debug Toolbar in to urls.py<\/h3>\n\n\n\n<p>To use the<strong> debug toolbar<\/strong> on the webpage, we must link to the URL paths of the webpages.<\/p>\n\n\n\n<p>Hence in urls.py, add the code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom django.conf import settings\nfrom django.URLS import path,include\n\nif settings.DEBUG:\n    import debug_toolbar\n    urlpatterns = &#x5B;\n        path(&#039;__debug__\/&#039;, include(debug_toolbar.urls)),\n    ] + urlpatterns\n<\/pre><\/div>\n\n\n<p>Make sure <strong>DEBUG <\/strong>is set to <strong>TRUE<\/strong>, in settings.py for this to work. <\/p>\n\n\n\n<p>Here we are adding the debug_toolbar to the URL patterns list, where all the other URLs are present<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"583\" height=\"291\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-39.png\" alt=\"Debug ToolBar urls.py\" class=\"wp-image-8251\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-39.png 583w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-39-300x150.png 300w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><figcaption>Debug ToolBar urls.py<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4) Enabling MiddleWare<\/h3>\n\n\n\n<p>The middleware associated with the Django Debug toolbar has to be added into the <strong>MIDDLEWARE <\/strong>section in settings.py<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMIDDLEWARE = &#x5B;\n    # ...\n    &#039;debug_toolbar.middleware.DebugToolbarMiddleware&#039;,\n]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5) Mentioning INTERNAL_IPS<\/h3>\n\n\n\n<p>The debug toolbar is shown only if the IP is present in the <strong>INTERNAL_IPS <\/strong>list in settings.py. For local development purposes, add the IP<strong> &#8216;127.0.0.1&#8217;<\/strong> into the list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nINTERNAL_IPS = &#x5B;\n    &#039;127.0.0.1&#039;,\n]\n<\/pre><\/div>\n\n\n<p>If the <strong>INTERNAL_IPS<\/strong> list is not already present, add the above list at the last of settings.py and add the IP &#8216;127.0.0.1.&#8217;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Output of the Django Debugging Tool<\/strong><\/h2>\n\n\n\n<p>With all the code added, visit <strong>127.0.0.1:8000\/sample\/<\/strong> in your preferred browser.<\/p>\n\n\n\n<p>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. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"841\" height=\"538\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-42.png\" alt=\"Debug toolbar\" class=\"wp-image-8260\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-42.png 841w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-42-300x192.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/image-42-768x491.png 768w\" sizes=\"auto, (max-width: 841px) 100vw, 841px\" \/><figcaption>Debug toolbar<\/figcaption><\/figure><\/div>\n\n\n\n<p>That&#8217;s it the tool bar will appear on the right side of the webpage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>That\u2019s it, guys!! This was all about the Django debugger too. Do check out the <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/django\/django-exception-handling\">Django Exceptions Handling<\/a> article to know more about debugging exceptions.<\/p>\n\n\n\n<p>See you in the next article!! Till then, keep practicing !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s get started !! The Art of Debugging No matter how professional, every [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":8255,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-8250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8250"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8250\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8255"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}