ChatGPT解决这个技术问题 Extra ChatGPT

How to debug Django commands in PyCharm

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.

If you're trying to debug manage.py commands (those in app/management/commands/ folder), you can use @Kevin's answer, but put in manage.py in the "Script", and the command you want to run in "Script parameters".

H
Henrik Heimbuerger

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à!


Emphasis for people like me who apparently can't read properly: the Host and Port fields have to be cleared, otherwise the custom command will not work!
Sorry, but what am I supposed to put into "custom run command?" I called mine "simple_chart" but I get "unknown command" when I try to debug. I have a method in views.py called simple_chart that I have placed a break point in. All I want is for the debugger to halt there so I can explore the variables in the local environment.
I don't have Django server option in the plus
For those who are struggling: the "custom run command" box should contain only your command. E.g. if you are running py manage.py cmd from console, this box will contain simply cmd.
@Dejell : you need the professional version of Pycharm for DJANGO support. ( jetbrains.com/pycharm/features/editions_comparison_matrix.html )
F
Fish Monitor

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.


This is what got it working for me, (pycharm 2016). Put manage.py in script, your django management command in script parameters (together with any django mangement command flags) and change the working directory.
S
Sandio

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


How does this differ from the accepted answer posted 7 years earlier?
@miken32 Looking back the first one has everything one needs, but for some reason, I did not get it. This answer accomplishes the same as the first one but uses an example. BTW some of us understand things explained differently.