-
VS Code에서 Django Debug 설정Backend/Django 2023. 10. 15. 22:54
2023.10.15
Pycharm에서 Django 디버그 를 진행했던 것처럼 VS Code에서 도 Django Debug가 가능한 설정을 진행하려고 함.
참고: https://code.visualstudio.com/docs/python/tutorial-django
Python and Django tutorial in Visual Studio Code
Python Django tutorial demonstrating IntelliSense, code navigation, and debugging for both code and templates in Visual Studio Code, the best Python IDE.
code.visualstudio.com
1. Python Interpreter를 Virtualenv 내부의 Viurtual Interpreter로 지정.
Ctrl + Shif + P → Python: Select Interpreter 선택 → 사용할 Virtualenv Interpreter 경로 입력.
2. Terminal 열기: Ctrl + Shift + ` → 자동으로 Virtualenv가 실행되어야 함.
그러나 이 과정에서 보안 오류가 발생함. (나중에 알게 된 사실이지만 VS Code docs 에서는 아래처럼 설명함.)
Note: On Windows, if your default terminal type is PowerShell, you may see an error that it cannot run activate.ps1 because running scripts is disabled on the system. The error provides a link for information on how to allow scripts. Otherwise, use Terminal: Select Default Profile to set "Command Prompt" or "Git Bash" as your default instead.
⇒ 윈도우 환경에서는 VS Code의 기본 Terminal이 PowerShell일 경우 시스템에서 스크립트 실행을 막도록 설정되어 있다고 함. 해결책은 1)PowerShell의 설정을 스크립트 실행 가능으로 변경하거나, 2) VS Code의 기본 Terminal 을 ‘Command Prompt’나 ‘Git Bash’로 변경하면 된다.
1)의 방법은 https://h-owo-ld.tistory.com/119 에 한글로 잘 설명되어 있음.
3. lanch.json 파일 생성
좌측 Run and Debug 메뉴에서 “create a launch.json file” 을 클릭→ 상단에 나타난 Select a debug configuration에서 Django 를 선택. launch.json 파일의 내용은 다음과 같아야 함. (*이 때 하위 경로를 의미하는 '\' 는 escape 하여 '\\' 로 입력해야 함.)
{ // 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, "justMyCode": true } ] }
파일은 VS Code에서 오픈한 폴더 최상위에 위치해야 한다.
💡 그러나 내 경우 디버그 하려는 프로젝트 폴더가 최상위 폴더가 아니었고, 최상위 폴더 아래에 여러 프로젝트로 구성되어 있는 형태였음. lanch.json에는 workspaceFolder에 있는 manage.py 를 실행하도록 설정되어 있음. 따라서 ${workspaceFolder} 를 내가 debug할 프로젝트 폴더 경로로 직접 수정함.
"program": "${workspaceFolder}\\manage.py"→ "program": "C:\\~\\{project}\\manage.py"이후 다시 좌측 Run and Debug 메뉴로 이동하면 시작 아이콘으로 표시된 Start Debugging 을 클릭하여 디버깅을 시작할 수 있음.
'Backend > Django' 카테고리의 다른 글
[QuerySet] Django 의 DB 접근법 (1): Lazy loading (3) 2024.11.10 filter() - fields loopkups(**kwargs), Q objects(**args) (2) 2023.12.03 PyCharm에서 Django 디버그 모드 활성화하기 (0) 2023.10.10 class Meta (2) 2023.09.18 django 입문(5) Model 3) DB에 Table 생성 (0) 2023.09.17