How to use visual studio code to debug django
Asked Answered
G

6

34

I'm new at django development and come from desktop/mobile app development with Xcode and related IDE.

I have to use Django and I was wondering if there was an efficient way to debug it using Visual Studio Code (or Atom).

Any help related to Django IDE would be helpful too.

Gujral answered 2/12, 2016 at 17:10 Comment(0)
C
61

For VSCode (full disclosure, I'm one of the VSCode developers) try installing the Python extension to get started.

This documentation covers debugging Django. There should be a included debug configuration or you can add your own to the launch.json file:

{
    "name": "Django",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "pythonPath": "${config.python.pythonPath}",
    "program": "${workspaceRoot}/manage.py",
    "args": [
        "runserver",
        "--no-color",
        "--noreload"
    ],
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput",
        "DjangoDebugging"
    ]
}

The Python extension also provide many other features that you may find useful.

Clarinda answered 2/12, 2016 at 21:1 Comment(6)
Thank you very much for your help :)Gujral
I find that Debug Console won't print django logs. I figure out that it is because VSCode doesn't support colored logs, so I think --no-color argument is also needed here.Ghostly
I could be doing this wrong, but I get multiple errors from this, starting with VS Code complaining about not recognizing the pair "type":"python"; full-disclosure, I do have MagicPython, Python, Python Extension Pack, and Python for VSCode installed and running.Kirchhoff
Is there a way to do this with auto reloading? It doesn't look like VSCode supports it but it's a pain to manually restart the server whenever I make a change. I think you could perhaps set an attach debugging config to attach to the port of a server running elsewhere. But I haven't been able to get it workingEmancipation
@dspacejs joining the party pretty late but in case someone still needs this: just remove the --noreload argument (specified in args).Uriniferous
I am using similar config to run a python module, this module is basically a flask app. When u put debugger red dot on any line in any controller the code execution does not pause there. Does anyone have any idea about that?Cory
Y
7

VSCode has an official tutorial explaining this:

https://code.visualstudio.com/docs/python/tutorial-django

There are several steps that need to be taken, which I don't all want to write out manually, since there are quite some steps, but I'll try to summarize what needs to be done:

The text below is basically a partial copy of the above tutorial, I am not claiming I came up with this myself.

1. Make sure to check out the prerequisites (use VS Code Python extension, install Python on local machine) link to docs

2. Use Python virtual environment link to docs

Besides using a Python virtual environment, you also need to select the Python executable inside this virtual environment as the interpreter in VS Code. This can be done like so:

In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)). Then select the Python: Select Interpreter

Then you select the Python executable inside your virtual environment, which you can recognize by it's path.

3. Create debugger lauch profile

as described here, in the documentation

upper left of the VS Code window)

4. Now you can start debugging

this part of the documentation will give you an introduction on how to do that

Yordan answered 6/5, 2019 at 14:27 Comment(1)
followed the exact steps of that tutorial, but I don't get breakpoints working. Breakpoints in manage.py are hit, but any breakpointrs in my views are just ignored. Any suggestions?Kanzu
L
3

Only experimental configuration works for me.

{
            "name": "Django",
            "type": "pythonExperimental",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "django": true
},

Standard config causes Unverified breakpoint issue.

Liminal answered 14/9, 2018 at 11:4 Comment(1)
Unlike the default option "type": "python", this gives me an error: The debug type is not recognized. Make sure that you have a corresponding debug extension installed and that it is enabled. Could you clarify if this requires an extension, and which is it? I have the default Python and Django development extensions installed already.Essequibo
T
2
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "runserver"
            ],
            "django": true
        },
        {
            "name": "Django: makemigrations",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "makemigrations"
            ],
            "django": true
        },
        {
            "name": "Django: migrate",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "migrate"
            ],
            "django": true
        },
    ]
}
Tortoiseshell answered 27/10, 2021 at 11:56 Comment(0)
L
1

Nothing worked for me until I had disabled auto reload (--noreload as an argument is crucial, not really sure why it causes problem with debugging)

Lifton answered 1/9, 2020 at 15:27 Comment(1)
--noreload still doesn't work for me unfortunately.Essequibo
I
0

If you are trying to use VS Code with Gunicorn to debug a Django application this launch.json worked for me.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Gunicorn: Django ",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/venv/bin/gunicorn",
      "gevent": true,
      "cwd": "${workspaceFolder}/src",
      "args": [
        "config.wsgi:application",
        "--bind=127.0.0.1:8000",
        "--reload",
        "--worker-class=eventlet",
        "--workers=1",
        "--threads=1",
        "--timeout=1800",
        "--reload-extra-file=${workspaceFolder}/src/assets/templates/base.html",
      ],
    },
  ]
}

These dependencies are required. Is recommended to add them to a requirements.txt file.

pip install --upgrade -r src/requirements.txt

pip
Django
gunicorn
eventlet
gevent

Gunicorn's --reload-extra-file does not allow wildcards. All extra files (html, css, js, etc.) to be monitored for changes must be added manually.

Irkutsk answered 29/7, 2024 at 4:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.