How to debug Django commands in PyCharm

DjangoPycharm

Django Problem Overview


I know how to run commands with PyCharm (Tools -> Run manage.py Task), but I would like to debug them also, including my commands and third party app's commands.

Django Solutions


Solution 1 - Django

You can debug a custom Django admin/management command in PyCharm by creating a custom Django server entry on the Run/Debug Configuration menu:

  • Click Edit Configurations....
  • Click the plus sign and choose Django server.
  • Fill in the Name as you please, clear the Host and Port fields, check Custom run command and enter the name of your command to the right of the checkbox.
  • Enter any extra command-line arguments into the separate field Additional options, not appended in the run command.
  • Click OK.

Now set a breakpoint, choose your new configuration from the Run/Debug Configuration menu and click the Debug button. Et voilà!

Solution 2 - Django

Since clearing Host and Port will not make the command run at all (PyCharm 5), the solution I found is to use a Python run configuration instead of a Django server. Fill Script with your manage.py script, other parameters in Script Parameters, and adjust your environment such as Working directory.

Solution 3 - Django

I am explaining using my following custom Django command:

python manage.py execute_algorithm -f input_data.json

Steps to configure Django Command: Step: From Django toolbar go to:

> Run > Edit Configurations

> Click on the '+' icon at top left, to create new command > select 'Django server' from the drop down.

Fill following details:

> Name: any suitable name that you want to give to this config e.g. execute_algorithm_command

> Host: Clear the field

> Port: It's 8000 by default, clear it.

> Custom Run Command: Check this box fist. Provide your command name there. You can get that from apps/module/management/command/execute_algorithm. e.g value: execute_algorithm

> Additional options: Whatever is there, after you command name. value is: -f input_data.json

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTaras BilynskyiView Question on Stackoverflow
Solution 1 - DjangoKevinView Answer on Stackoverflow
Solution 2 - DjangoFish MonitorView Answer on Stackoverflow
Solution 3 - DjangoSandioView Answer on Stackoverflow