如何在Visual Studio Code中使用django shell调试django(逻辑)代码

问题描述 投票:0回答:1

我正在开发一个django应用程序,包括使用Visual Studio Code的自定义逻辑代码,我想通过django shell在interactie中调试我的代码。这是可能的,如果是这样,需要什么调试设置?

visual-studio-code django debugging
1个回答
0
投票

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()

然后使用常规的Python: Current file调试选项运行此文件

© www.soinside.com 2019 - 2024. All rights reserved.