amerkurev / django-docker-template

Dockerized Django with Postgres, Gunicorn, and Traefik or Caddy (with auto renew Let's Encrypt)
https://django-docker.dev
MIT License
169 stars 33 forks source link

Feature: Light VSCode Integration #18

Closed nbovee closed 1 month ago

nbovee commented 1 month ago

Hello,

I've been working with this template a good amount lately, and polished up some of the features I set up for some beginners for possible inclusion.

notes:

platform linux -- Python 3.11.9, pytest-7.4.4, pluggy-1.5.0
django: version: 4.2.10, settings: website.settings (from ini)
rootdir: /usr/src/website
configfile: pytest.ini
plugins: django-4.7.0
collected 10 items

polls/tests.py ..........                                                [100%]

============================== 10 passed in 0.19s ==============================
Name                           Stmts   Miss  Cover   Missing
------------------------------------------------------------
polls/__init__.py                  0      0   100%
polls/admin.py                    12      0   100%
polls/apps.py                      4      0   100%
polls/models.py                   20      2    90%   15, 33
polls/tests.py                    57      0   100%
polls/urls.py                      4      0   100%
polls/views.py                    28      8    71%   39-58
setup/__init__.py                  0      0   100%
setup/admin.py                     1      0   100%
setup/apps.py                      4      0   100%
setup/management/__init__.py      18     11    39%   10-13, 17-25
setup/models.py                    1      0   100%
setup/tests.py                     1      0   100%
website/__init__.py                6      0   100%
website/settings.py               52      2    96%   95, 198
website/urls.py                    6      0   100%
------------------------------------------------------------
amerkurev commented 1 month ago

Hi @nbovee ! Thanks for the great work, it's awesome! I'll merge the changes into master as soon as I can review everything. Unfortunately, I don't use VS Code often. Could you please confirm that all your changes related to VS Code work for you?

nbovee commented 1 month ago

Thank you, I'd be happy to!

What do you prefer I add to the readme, if anything? I was hesitant to as I didn't want to get into too much extra detail.

Here is the log of running the vscode debug task through the Run and Debug tab (twice):

 *  Executing task: docker-build 

> docker image build --pull --file "C:\CODE\django-docker-template/Dockerfile" --tag "django-docker-template:master" --label "com.microsoft.created-by=visual-studio-code" "C:\CODE\django-docker-template" <

#0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 2.03kB done
#1 DONE 0.0s

...shortened log...

#16 DONE 0.0s

View build details: docker-desktop://dashboard/build/default/default/k6jqqt6vli6mtdrolelmjzqw3
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: docker run --rm -v sqlite:/sqlite django-docker-template:master python manage.py safecreatesuperuser --no-input 

SQLite will be used.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK

...shortened log...

  Applying sessions.0001_initial... OK
Admin user created
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: docker-run: debug 

> docker container run --detach --tty --rm --name "djangodockertemplate-dev" --publish "8000:8000" --mount "type=bind,source=c:\Users\USER\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy,destination=/debugpy,readonly" --label "com.microsoft.created-by=visual-studio-code" --env "DJANGO_DEBUG=True" --env "POSTGRES_DB=" --env-file "C:\CODE\django-docker-template/.env" --entrypoint "python3" -v sqlite:/sqlite django-docker-template:master <

4827461a50009b8f0e6379f604db773f32f4c204479ceb48e148e04c15554616
 *  Terminal will be reused by tasks, press any key to close it. 

At this point the terminal automatically tabs to the debug console, and automatically launches localhost:8000 in the browser. The first warning seems due to the way vscode attaches debugpy, but causes no problems. We also get the 404 as the base index does not exist.

0.59s - Expected: /debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_linux_amd64.so to exist.

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 31, 2024 - 20:36:23
Django version 4.2.10, using settings 'website.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

"GET / HTTP/1.1" 404 2292

Breakpoints marked on vscode are correctly caught and mapped. image

When the task is run a second time, the only change in our log is the altered output of safecreatesuperuser:

 *  Executing task: docker run --rm -v sqlite:/sqlite django-docker-template:master python manage.py safecreatesuperuser --no-input 

SQLite will be used.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  No migrations to apply.
Admin user already exists
 *  Terminal will be reused by tasks, press any key to close it. 

Otherwise, the second and future runs function as above.

If the user directly invokes Tasks: Run Task > docker-run: debug, they do not get the debugger, but no adverse behavior is caused. At worst they would need docker stop djangodockertemplate-dev to stop the container.

 *  Executing task: docker-build 

> docker image build --pull --file "C:\CODE\django-docker-template/Dockerfile" --tag "django-docker-template:master" --label "com.microsoft.created-by=visual-studio-code" "C:\CODE\django-docker-template" <

  ...shortened log...

#16 DONE 0.0s

View build details: docker-desktop://dashboard/build/default/default/i09lctw92nw1k9tnjg73cfxij
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: docker run --rm -v sqlite:/sqlite django-docker-template:master python manage.py safecreatesuperuser --no-input 

  ...shortened log...

  Applying sessions.0001_initial... OK
Admin user created
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: docker-run: debug 

> docker container run --detach --tty --name "djangodockertemplate-dev" --publish "8000:8000" --mount "type=bind,source=c:\Users\USER\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy,destination=/debugpy,readonly" --label "com.microsoft.created-by=visual-studio-code" --env "DJANGO_DEBUG=True" --env "POSTGRES_DB=" --env-file "C:\CODE\django-docker-template/.env" --entrypoint "python3" -v sqlite:/sqlite django-docker-template:master <

311f04e52ff3bbfbf7423084473b5ef4e7ab9722c49a981ede82fb299acae9fe
 *  Terminal will be reused by tasks, press any key to close it. 

Here is the updated output of pytest.sh, which can be run with Tasks: Run Task > shell: pytest.sh in addition to the provided readme command:

============================= test session starts ==============================
platform linux -- Python 3.11.9, pytest-7.4.4, pluggy-1.5.0
django: version: 4.2.10, settings: website.settings (from ini)
rootdir: /usr/src/website
configfile: pytest.ini
plugins: django-4.7.0
collected 10 items

polls/tests.py ..........                                                [100%]

============================== 10 passed in 0.20s ==============================
Name                           Stmts   Miss  Cover   Missing
------------------------------------------------------------
polls/__init__.py                  0      0   100%
polls/admin.py                    12      0   100%
polls/apps.py                      4      0   100%
polls/models.py                   20      2    90%   15, 33
polls/tests.py                    57      0   100%
polls/urls.py                      4      0   100%
polls/views.py                    28      8    71%   41-60
setup/__init__.py                  0      0   100%
setup/admin.py                     1      0   100%
setup/apps.py                      4      0   100%
setup/management/__init__.py       0      0   100%
setup/models.py                    1      0   100%
setup/tests.py                     1      0   100%
website/__init__.py                6      0   100%
website/settings.py               52      2    96%   99, 202
website/urls.py                    6      0   100%
------------------------------------------------------------
TOTAL                            196     12    94%
amerkurev commented 1 month ago

Hi @nbovee! Awesome, thank you! I'll merge this code into the master branch. If you get a chance, it would be great to add an extra section in the README about how to work with the project in VS Code. But only if you feel like it! 😊