My Django dev utilities

besil

besil

Posted on January 2, 2022

My Django dev utilities

My Django dev utilities

These are some useful scripts I usually use while developing Django+Svelte apps. You can find the code here.

The sample app is described in my previous article: My Django-Svelte setup for fullstack development

.gitignore

My .gitignore is the following

echo '.venv
.vscode
.idea
db.sqlite3
__pycache__
**.pyc
staticfiles
.coverage
.pytest_cache' > .gitignore
Enter fullscreen mode Exit fullscreen mode

Dockerfile

mkdir docker
touch docker/Dockerfile
touch docker/entrypoint.sh
Enter fullscreen mode Exit fullscreen mode

docker/Dockerfile

FROM python:slim
ENV PYTHONUNBUFFERED 1
RUN pip install poetry

RUN mkdir /code
WORKDIR /code

COPY pyproject.toml /code
COPY poetry.lock /code
RUN poetry install

ADD frontend /code/frontend/
ADD myapp /code/myapp/
COPY manage.py /code/
ADD docker/entrypoint.sh /code

#ENTRYPOINT ["/bin/bash"]
CMD ["bash", "entrypoint.sh"]
Enter fullscreen mode Exit fullscreen mode

docker/entrypoint.sh

PORT=${1:-8000}
poetry run python manage.py makemigrations
poetry run python manage.py migrate
poetry run python manage.py collectstatic --noinput
poetry run gunicorn --bind 0.0.0.0:$PORT myapp.wsgi
Enter fullscreen mode Exit fullscreen mode

Dev scripts

mkdir -p scripts
echo 'export DJANGO_DEBUG=ON
export LOGGING_LEVEL=DEBUG
poetry run python manage.py "$@"' >> scripts/manage.sh
echo "bash scripts/manage.sh runserver" >> scripts/run-dev-server.sh
echo "bash scripts/manage.sh makemigrations" >> scripts/migrate.sh
echo "bash scripts/manage.sh migrate" >> scripts/migrate.sh

mkdir -p scripts/docker/
echo "docker build -f docker/Dockerfile -t myapp:latest ." > scripts/docker/build.sh

echo "docker run -it --rm -p 8000:8000 myapp:latest" >> scripts/docker/run.sh
Enter fullscreen mode Exit fullscreen mode

Test scripts

poetry add pytest pytest-cov pytest-xdist pytest-django pytest-watch --dev
Enter fullscreen mode Exit fullscreen mode
echo "[pytest]
log_cli = 1
DJANGO_SETTINGS_MODULE = myapp.settings
python_files = tests.py test_*.py *_tests.py" > pytest.init
Enter fullscreen mode Exit fullscreen mode
echo "export DJANGO_DEBUG=ON
export LOGGING_LEVEL=DEBUG
poetry run pytest -n 4 --cov=myapp $1" > scripts/run-tests.sh
Enter fullscreen mode Exit fullscreen mode
echo 'export DJANGO_DEBUG=ON
export LOGGING_LEVEL=DEBUG
poetry run ptw -c -p --runner "pytest --last-failed --new-first $1"' > scripts/run-tests-watch.sh
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
besil
besil

Posted on January 2, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related