Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 18, 2022 04:52 pm GMT

Makefile for your Django project

During the development process, you often need to run some commands in your terminal: create migrations, run tests, linters, etc. Usually, you execute these commands regularly.

It's helpful to have shortcuts for such commands. And even better to share them among other developers on the project. For this goal, I use Makefile. Here is the list of useful commands for a Django project.

Installing dependencies

I'm not a big fan of Poetry. It has some problems with Dependabot and doesn't play with Renovate at all. I use pip-tools with separate requirements.in and requirements-dev.in for local and prod environments.

pip-install-dev:    pip install --upgrade pip pip-tools    pip-sync requirements.txt requirements-dev.txtpip-install:    pip install --upgrade pip pip-tools    pip-sync requirements.txtpip-update:    pip install --upgrade pip pip-tools    pip-compile requirements.in    pip-compile requirements-dev.in    pip-sync requirements.txt requirements-dev.txt

Commands:

  • pip-install-dev: Installs all requirements, including local. It is the main command for installing the project's dependencies, so I put this command at the top.
  • pip-install - I don't use this command often, but it could be helpful to run your code with production dependencies only.
  • pip-update Updates your requirements.txt and requirements-dev.txt after you add a new package to the requirements.in or requirements-dev.in.

Running the project

server:    python manage.py migrate && python manage.py runserverworker:    python -m celery -A project_name worker --loglevel infobeat:    python -m celery -A project_name beat --loglevel info

That is an obvious one. The commands to run local web server and Celery. Also, I like to apply new migrations automatically when I run the server.

Running linters

lint:    flake8 palyanytsya    mypy palyanytsyablack:    python -m black palyanytsyacleanimports:    isort .    autoflake -r -i --remove-all-unused-imports --ignore-init-module-imports project_nameclean-lint: cleanimports black lintcheckmigrations:    python manage.py makemigrations --check --no-input --dry-run

Commands:

  • lint: Runs flake8 linter and mypy type checker.
  • black: Automatic code formatting with Black.
  • cleanimports: runs isort and removes unused imports with Autoflake. Be sure to set up profile=black in isort settings to avoid conflicts with Black.
  • clean-lint: run all the stuff above. You can run this command before committing to formatting your code properly.
  • checkmigrations: prevents you from committing model changes without migrations. Really cool stuff!

Also, I use make lint && make checkmigrations in the CI pipeline and in the git pre-commit hook. You can also create a command for setting up such a hook:

install-hooks:    echo "make lint && make checkmigrations" > .git/hooks/pre-commit && chmod 777 .git/hooks/pre-commit

Running tests

test:    pytest -n 4 -x

Runs pytest in multiprocess mode using pytest-xdist.

Compiling messages

messages:    python manage.py makemessages --all --ignore=venv --extension html,py    python manage.py translate_messages -s en -l uk -l es -u    python manage.py compilemessages --ignore venv

Collects all string literals for translation. Also, I use Django Autotranslate to translate phrases using Google Translate automatically.

The end

As long as the project goes on, new local commands appear. Maybe, you need to shell into the dev server to run some python code. Or download a fresh database dump (without sensitive data) for local bug reproducing. It's better to keep this knowledge in the codebase, not in your head. Also, you won't need to explain how to do exactly the same operation to your colleagues.

For more complex tasks, you can even create a shell script and call it from Makefile. So, it would be easier to find this new command for other devs.


Original Link: https://dev.to/daiquiri_team/makefile-for-your-django-project-577n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To