Debugging a Django Application with PyCharm Community

Mangabo Kolawole - May 13 '22 - - Dev Community

PyCharm is a Jetbrains product focusing on the development of Python projects. It also comes with robust support for debugging in the applications you are writing and Django.

Even if the professional version definitely has better support for Django, you can use the Community version and configure it for debugging.

Let's see how to do it.

Creating a Debug configuration

Open your Django application using PyCharm and make sure to configure a virtual environment. You can learn more about it here.

To create a debug script, click on Run in the navigation bar and choose "Edit Configuration". You'll have a similar screen.

Screenshot from 2022-05-12 16-07-13.png

We have no configuration for this project. Let's create a configuration to run a Django application.

Adding a Django configuration

The next step is to click on "Add new run configuration" and choose "Python" for the option. You'll be presented with another widget where you have to add some basic configuration so PyCharm can know how to run your application.

Screenshot from 2022-05-12 16-08-05.png

The configuration is done, and we are nearly done. The Pycharm version comes with handy options, and feel free to modify it to your needs.

Once you are done, click on Apply, and congrats! You have your first run configuration in Django.

Running a Django application in Debug mode

Pycharm offers two ways to run the Django application. In Run mode or Debug mode.

The Debug mode helps you place a breakpoint on your application's code and analyze the state of your application.
You get access to the frames and variables plus their values. Pretty cool, unh? Let's try it.

Press Shift + F9 to run the configuration in Debug mode. You can also directly click on the Bug sign on the top bar.

Screenshot from 2022-05-12 16-08-41.png

Add some breakpoints in the code of the Django project. In my case, I've added a breakpoint in a viewset of my Django projects.
Then, I requested the endpoint that this viewset handles. You'll have something similar with different states and frames.

Screenshot from 2022-05-12 16-39-30.png

Your Django application has stopped, and you can analyze the application's state. To resume, press F9.

Article posted using bloggu.io. Try it for free.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player