Using Debug Mode in VS Code
In this video, we are going to learn how to run our django project in debug mode in Visual Studio Code.
At first, let us try to understand what is Debug Mode.
So, debug mode is the state of running our django application where we can monitor the program execution line by line, track the erros and exceptions that are raised during execution of program, set breakpoints and trace down the state of application and many more.
When you run a Django application in debug mode in Visual Studio Code, then you can get a bunch of features which is really helpful when you are developing a real world production grade application like:
-
Breakpoints: A breakpoint is a certain point in your code where the debugger will pause the execution of program. This allows us to inspect the program's state, variable values, and control flow at that specific point.
-
Stepping Through Code: Debugging allows you to step through your code line by line. We can move forward one line at a time, step into function calls to understand their behavior, and step out of functions to return to the caller's context.
-
Variable Inspection: While debugging, we can inspect the values of variables at different points in your code. This helps us to understand how values change as the program runs and then you can locate any unexpected behavior.
-
Call Stack: The call stack shows you the sequence of function calls that led to the current point in the code. This is particularly useful for understanding the context in which a certain function is being called.
-
Exception Handling: Debug mode provides better visibility into exceptions (runtime errors) that occur in your code. We can see the full stack trace and pinpoint the exact location where the exception was raised.
-
Console Interaction: While debugging, you can interact with the program using a debugging console. This allows us to execute code snippets in the context of your program, which can be helpful for quick testing and diagnosis.
-
Debugging Toolbar: VS Code provides a debugging toolbar that includes buttons for stepping through code, stopping execution, and interacting with the debugger's features.
Code Setup
-
So, now, lets run our django project in debug mode. But at first, lets setup the project code for this section.
-
So, you need to download the project code files the resources section: [DOWNLOAD ZIP FILE](../assets/code/Section%2019/Section%2019%20Code%20Files.zip
-
Now, extract the folder on the desktop.
-
Then, copy the mysite folder which is the project root directory.
-
Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.
-
Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.
-
Run the migrations and create a superuser.
-
Create 5 Fake vaccine, center, storage, campaigns.
Live Demo on Debug Mode
- Click on Debug Mode and run it.
- Copy mysite relative path and paste in the dialog box.
- Open Signup view and set the debugger.
- Set a breakpoint and send get request from browser.
- Set a breadkpoint inside post request code and send post request from browser.
- Stop the debugger.
Debugger is one of the most important skill which every django developer should know. I use debugger everyday in my work. So, try to use debugger while building projects and it will really help you to debug some common and some complicated issues.