Pim*_*Pim 2 django debugging visual-studio-code
我正在使用 Visual Studio Code 开发一个包含自定义逻辑代码的 django 应用程序,我想在通过 django shell 进行交互时调试我的代码。这可能吗,如果可以,需要哪些调试设置?
小智 10
你绝对可以在 Django Shell 中进行调试。launch.json
VSCode 中 Django 调试的典型配置已manage.py runserver --noreload
用于启动开发服务器,因此您所要做的就是添加一个额外的调试配置manage.py shell
来代替,如下所示(可能需要根据您的项目结构进行调整):
{
"name": "Django Shell",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"shell"
]
}
Run Code Online (Sandbox Code Playgroud)
VSCode 将在其内置终端中启动 Django shell。
(向有关调试管理命令的相关讨论致敬,当我寻找这个问题的答案时,它为我指明了正确的方向。)
Shell 就是 shell,而 VSCode 就是 VSCode。您无法从 shell 调试代码。
当我需要调试我的自定义 Django 代码时,我将debug.py
文件放在我的项目根目录(在哪里manage.py
)并手动加载我的 Django 项目,即我模仿 Django shell。
# Here you should use all the logic that you have
# in manage.py before execute_from_command_line(sys.argv)
# Generally there is only settings module set up:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
# Initialize django application
import django
django.setup()
# Do what you want to debug and set breakpoints
from django.contrib.auth.models import User
User.objects.exists()
Run Code Online (Sandbox Code Playgroud)
然后只需使用常规Python: Current file
调试选项运行此文件
UPD: 现在 Django 的这个用例已记录在案:https ://docs.djangoproject.com/en/3.0/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
归档时间: |
|
查看次数: |
1114 次 |
最近记录: |