Debugging a Django web application in VS Code

Published on May 12,2020 by Maulik

Web applications, when built in their entirety, are a complex piece of software passing the execution of the code from one file to another within the project. It involves a lot of configuration and parameters, and frequently after making a change in a project file, we tend to forget to make necessary changes elsewhere in the project. And it becomes a tiresome process sometimes to find out what is causing our application to halt and then solve it.

Fortunately, we have a tool at our disposal, which makes it easy for us to quickly find out where and at what point is the error occurring in the web application. It becomes an essential tool as the project becomes more significant and more complex. VS Code has got a potent and customizable debugger tool, which helps us debug our code efficiently and effectively. I am explaining how you can configure and make use of the Debugger by giving a step-by-step guide in an example Django project using VS Code. You can download the example project from <here> to follow along. Please note that debuggers are generally useful for a complex project, so I give you a head start with a straightforward example, but the steps are the same for large projects as well.

In this article, you are learning: 

  • What is Debugger?
  • How to configure Debugger in VS Code for Django project
  • How to Debug code using our configured Debugger.

What is Debugger?

A debugger is a tool that allows us to control and monitor the execution of our code, like the state of our program—for example, knowing the value of the variables and few other things in the project. It offers functionality to stop at a particular point of the code where we think the error is occurring and observe the values of all the defined variables at that point of the code and proceed further in the code step-by-step. It enables us to carefully analyze our code line-by-line and see for ourselves with a “magnifying lens” where the code is deviating from the expected behavior.

How to configure Debugger for Django in VS Code?

  • Download the example project from the given link and open the downloaded project folder using the VS Code. Open the Debugger in side-panel by clicking on the debugger icon, as shown below, or you can press ‘Ctrl + Shift + D’ to open the debug side panel.
  • django debugger vs code
  • In the left corner of the debugger panel, you can see a green button that starts the Debugger. Adjacent to it is the selected configured service to run when that green button is pressed. In my case, I have already selected Python Django. If you want to add configuration for Django, you can click on dropdown and press Add Configuration, and it opens the launch.json file in the editor. Write python there, and you get a list to configure python with different applications, as shown below.
  • django debugger vs code
  • Now you can change the default configuration if you want to execute different files by providing a full path of the file. You can also remove or add arguments you want to pass while running your project. E.g., As shown in below animation, I added an argument so that my application runs on port 8090 instead of default port 8000 
  • django debugger vs code

How to Debug the Code using the configured Debugger?

After configuring the Debugger as per our project requirement, let’s start debugging the actual code and unleash the real power of our Debugger now.

  • Our project tree looks something like this: 

 <Project Tree>

  • I have configured my Django server to run on http://0.0.0.0:8090/, but you can configure it to whatever else you’d like. Now, go to the link http://0.0.0.0:8090/register. We start debugging the registration process of the Django project now.
  • django debugger vs code
  • If you see in the project, this register API request is handled in Sign_up/views.py in the register method. Let’s try to run the Debugger by selecting Run > Start Debugging or by clicking that green button in the debugger panel on the top left or by simply pressing F5. If a debugger is running successfully, you can see the status bar change from blue to orange, as shown below.
  • django debugger vs code
  • You can see a debugging toolbar as below with 6 options, namely Pause, Step Over, Step Into, Step Out, Restart, and Stop.
  • django debugger vs code
  • Now, to monitor the execution process, we are setting breakpoints in the register method. Breakpoints are points where the code halts execution when it reaches that specific line. To add the breakpoint, you can hover the cursor over the line margin, and a faded red dot is displayed. Just click on the line, and the breakpoint is set.
  • When we go to the register page, we send a “GET” request, and when we click on the “Sign Up” button after filling the form, we send a “POST” request. 
  • We are adding a breakpoint inside the first “if” condition, which checks for the POST request. So code execution can only reach there if the “Sign up” button is clicked. Otherwise, it enters an “else” statement where we add our 2nd breakpoint. On receiving the “POST” request, it is executed. We are adding a breakpoint in between inside the 2nd “if” condition, which will be executed only if the form filled is valid. 
  • django debugger vs code
  • Now reload the register page, and we reach the 3rd breakpoint inside the else condition because we just sent a “GET” request to our API by reloading the page. The yellow line here shows that it is the next line to be executed.
  • django debugger vs code

 

  • The side panel for the variable looks something like this. It only contains a request variable 
  • django debugger vs code
  • In the Debugging toolbar, the press continues (F5), so the code again starts the execution until it meets a breakpoint or reaches return. In our case, it reaches the return.
  • Now, let’s fill the form correctly as per the given instructions and click on the “Sign Up” button. We encounter the 1st breakpoint inside the “if” condition because the request we sent just now was a “POST” request.
  • django debugger vs code
  • As we have filled a valid form, it must also get to the third breakpoint, which is displaying the success message.
  • django debugger vs code

  

  • If everything was successful, you are redirected to the login page and can see the account created message, as shown below.
  • django debugger vs code
  • And our variable values at this state are as below. The form contains UserRegistrationForm object with the details of the user, and we have a request object with POST request.
  • django debugger vs code
  • You can also see the call_stack displaying which functions have called which functions until the currently executing function.
  • django debugger vs code
  • If you want to observe a specific variable, you can also add that variable to watch. The value can change that variable as the program is executed. Suppose at this state of the program, my watch window with the variable form looks like something like this.
  • django debugger vs code

Summary

A debugger is an essential tool as the project becomes more difficult in structure and size. In the VS Code, we learned how to configure the Debugger as per our requirement and how to debug the code. Just for fun, you can try filling the form with invalid details and monitor how the program flow is executed using the Debugger as explained above. I hope this tutorial is of value to you. Thank you for reading this article till the end.

1 Comments

jyqwmmrthw

3 months, 3 weeks

Django Circle | Debugging a Django web application in VS Code jyqwmmrthw http://www.g2q0l8p28n6u7ke1kz1065y1h4yqk6i5s.org/ [url=http://www.g2q0l8p28n6u7ke1kz1065y1h4yqk6i5s.org/]ujyqwmmrthw[/url] <a href="http://www.g2q0l8p28n6u7ke1kz1065y1h4yqk6i5s.org/">ajyqwmmrthw</a>

Related Articles

Deploy Django on Google Compute Engine with Nginx, Gunicorn and Postgresql (Google Cloud SQL)

Published on May 18,2020 by Maulik

Deploy Django on Google Compute Engine with Nginx, Gunicorn and Postgresql (Google Cloud SQL)

Google Cloud Platform is one of the favorite platforms to deploy web applications. In order to deploy Django Application, we need a Virtual Machine and …

Read full article

How to setup Django, Gunicorn, Nginx and PostgreSQL service using docker compose?

Published on October 09,2020 by Maulik

How to setup Django, Gunicorn, Nginx and PostgreSQL service using docker compose?

Docker helps to simplify and set up a uniform platform for development, staging, and production environments. DevOps efforts are reduced by using docker technology. This …

Read full article

How to create a common response format for 200, 400, 500 responses by creating custom exception handler in Django Rest Framework?

Published on May 28,2020 by Maulik

How to create a common response format for 200, 400, 500 responses by creating custom exception handler in Django Rest Framework?

In micro-services architecture, multiple client applications are consuming the backend API. The backend server does the core business logic and all heavy lifting. The client …

Read full article

copied to clipboard

Sign up for our newsletter

Please join our news letter which we share every month, you would love interesting python and django news letters.

We understand no one like spamming, your emails are safe with us.

Copyright © Django Circle All Rights Reserved.