{"id":9705,"date":"2023-09-21T11:28:28","date_gmt":"2023-09-21T09:28:28","guid":{"rendered":"https:\/\/via-internet.de\/blog\/?p=9705"},"modified":"2023-09-21T11:34:56","modified_gmt":"2023-09-21T09:34:56","slug":"django-debugging-django-app-in-vs-code","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2023\/09\/21\/django-debugging-django-app-in-vs-code\/","title":{"rendered":"Django | Debugging Django-App in VS Code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">See <a href=\"https:\/\/code.visualstudio.com\/docs\/python\/tutorial-django\" data-type=\"link\" data-id=\"https:\/\/code.visualstudio.com\/docs\/python\/tutorial-django\" target=\"_blank\" rel=\"noreferrer noopener\">here <\/a>how to configure VS Code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Switch to\u00a0<strong>Run<\/strong>\u00a0view in VS Code (using the left-side activity bar or\u00a0F5). You may see the message <br><em>&#8220;To customize Run and Debug create a launch.json file&#8221;. <\/em><br>This means that you don&#8217;t yet have a\u00a0<code>launch.json<\/code>\u00a0file containing debug configurations. VS Code can create that for you if you click on the\u00a0<strong>create a launch.json file<\/strong>\u00a0link:<img decoding=\"async\" src=\"https:\/\/code.visualstudio.com\/assets\/docs\/python\/shared\/debug-panel-initial-view.png\" alt=\"Django tutorial: initial view of the debug panel\"><\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Select the link and VS Code will prompt for a debug configuration. Select\u00a0<strong>Django<\/strong>\u00a0from the dropdown and VS Code will populate a new\u00a0<code>launch.json<\/code>\u00a0file with a Django run configuration. <br>The\u00a0<code>launch.json<\/code>\u00a0file contains a number of debugging configurations, each of which is a separate JSON object within the\u00a0<code>configuration<\/code>\u00a0array.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scroll down to and examine the configuration with the name &#8220;Python: Django&#8221;:<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"version\": \"0.2.0\",\n  \"configurations\": [\n    {\n      \"name\": \"Python: Django\",\n      \"type\": \"python\",\n      \"request\": \"launch\",\n      \"program\": \"${workspaceFolder}\\\\manage.py\",\n      \"args\": [\"runserver\"],\n      \"django\": true,\n      \"justMyCode\": true\n    }\n  ]\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This configuration tells VS Code to run\u00a0<code>\"${workspaceFolder}\/manage.py\"<\/code>\u00a0using the selected Python interpreter and the arguments in the\u00a0<code>args<\/code>\u00a0list. <br>Launching the VS Code debugger with this configuration, then, is the same as running\u00a0<code>python manage.py runserver<\/code>\u00a0in the VS Code Terminal with your activated virtual environment. (You can add a port number like\u00a0<code>\"5000\"<\/code>\u00a0to\u00a0<code>args<\/code>\u00a0if desired.)<br>The\u00a0<code>\"django\": true\u00a0<\/code>entry also tells VS Code to enable debugging of Django page templates, which you see later in this tutorial.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Test the configuration by selecting the\u00a0<kbd><strong>Run<\/strong>\u00a0>\u00a0<strong>Start Debugging<\/strong><\/kbd>\u00a0menu command, or selecting the green\u00a0<strong><kbd>Start Debugging<\/kbd><\/strong>\u00a0arrow next to the list (F5):<img decoding=\"async\" src=\"https:\/\/code.visualstudio.com\/assets\/docs\/python\/django-tutorial\/debug-continue-arrow.png\" alt=\"Django tutorial: start debugging\/continue arrow on the debug toolbar\"><\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ctrl+click&nbsp;the&nbsp;<code>http:\/\/127.0.0.1:8000\/<\/code>&nbsp;URL in the terminal output window to open the browser and see that the app is running properly.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Close the browser and stop the debugger when you&#8217;re finished. To stop the debugger, use the Stop toolbar button (the red square) or the\u00a0<kbd><strong>Run<\/strong>\u00a0>\u00a0<strong>Stop Debugging<\/strong><\/kbd>\u00a0command (Shift+F5).<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can now use the\u00a0<kbd><strong>Run<\/strong>\u00a0>\u00a0<strong>Start Debugging<\/strong><\/kbd>\u00a0at any time to test the app, which also has the benefit of automatically saving all modified files.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>See here how to configure VS Code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[21,65],"tags":[],"class_list":["post-9705","post","type-post","status-publish","format-standard","hentry","category-django","category-python"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=9705"}],"version-history":[{"count":4,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9705\/revisions"}],"predecessor-version":[{"id":9710,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9705\/revisions\/9710"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=9705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=9705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=9705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}