django-command tool

Django-command is a command line tool that allows you to execute frequently used commands when developing projects on the Django framework. This tool helps to increase the efficiency of development and simplify the workflows associated with web applications on Django.

Who is this article for?

The article is intended for developers of projects on the Django framework who want to improve the efficiency of their work.

Introduction

When developing projects on the Django framework, I often had to perform the same type of routine tasks: creating migrations, applying migrations, updating locales, and so on every time I changed database models. I had to remember a large set of commands and their arguments. Of course, I stored all the commands in one file and dragged this file from project to project. As the number of projects grew, it became inconvenient for me to look into the file every time to find the right command. I wanted a more convenient tool that would optimize my work. I did not find a suitable solution to this problem on the Internet. As a result, I decided to create my own tool django-command, which would provide a convenient interactive interface, as well as the ability to run from the console without interaction.

Installation and use

You can use pip to install django-command:

pip install django-command

There are several ways to run commands:

  1. In the terminal, type django-command, after that a list of available commands for execution will appear in interactive mode, mark which commands to execute separated by a space. The marked commands will be executed in the order of selection.

    (venv) PS .\> django-command
    [?] Select 1 or more commands:
     > [ ] create_local                   [1] Creating locales (ru, en)
       [ ] update_local                   [2] Updating and compiling locales
       [ ] collect_static                 [3] Assembling static files in the STATIC_ROOT folder
       [ ] make_migrations                [4] Creating migrations
       [ ] make_migrations_app            [5] Creating the first migration for the application
       [ ] make_empty_migrations_app      [6] Create a blank migration for the application. Used to add default data to the database table
       [ ] migrate                        [7] Applying migrations [--db_label default]
       [ ] create_superuser               [8] Creating a user with superuser rights
       [ ] create_app                     [9] Creating an application
       [ ] run_server                     [10] Running a project on a port number, or ipaddr:port (default "127.0.0.1:8000") [--port 127.0.0.1:8000]
       [ ] install_requirements           [11] Install all dependencies for a project from a file (default "requirements.txt")
       [ ] print_requirements             [12] Automatically generates all the necessary dependencies for the project, and also allows you to save this list to a file (default "requirements.txt") [--save_in_file requirements.txt]
  2. Commands can be executed by their name or by number.

    django-command make_migrations migrate
    # or with argument
    django-command make_migrations migrate -db default
    # or
    django-command 4 7

Descriptions of commands and arguments

To view the list of available commands, enter in the terminal.

(venv) PS .\> django-command -h
usage: django-command [-h] [-db DB_LABEL] [-s SAVE_IN_FILE] [-p PORT] [-v] commands [commands ...]   

CLI tool that allows you to run commonly used commands when developing Django projects.              

positional arguments:                                                                                
 commands              commands to run: create_local, update_local, collect_static, make_migrations,
                       make_migrations_app, make_empty_migrations_app, migrate, create_superuser,   
                       create_app, run_server, install_requirements, print_requirements             

optional arguments:
 -h, --help            show this help message and exit
 -db DB_LABEL, --db_label DB_LABEL
                       database label for "migrate" command
 -s SAVE_IN_FILE, --save_in_file SAVE_IN_FILE
                       save to file for "print_requirements" command
 -p PORT, --port PORT  port number, or ipaddr:port for "run_server" command
 -v, --version         show program's version number and exit

Commands in django-command

Analogue of the command in django

create_local

django-admin makemessages -l

Creates locale files (ru, en) for internationalization.

update_local

django-admin makemessages -a

django-admin compilemessages

Updating and compiling locales.

collect_static

python manage.py collectstatic

Collects static files in the STATIC_ROOT folder.

make_migrations

python manage.py makemigrations

Creates migrations for all applications.

make_migrations_app

python manage.py makemigrations {app_name}

Creates migrations for a specific application.

make_empty_migrations_app

python manage.py makemigrations –empty {app_name}

Creates an empty migration for a specific application.

migrate

python manage.py migrate

Apply migrations. Updates the database schema.

create_superuser

python manage.py createsuperuser

Creates a user with administrator rights.

create_app

python manage.py startapp {app_name}

Creates a new application inside a Django project.

run_server

python manage.py runserver

Runs a web server for development and also serves static files.

install_requirements

pip install -r {file_name}

Installs all dependencies for the project from a file (default “requirements.txt”)

print_requirements

pip freeze > {file_name}

Automatically generates all necessary dependencies for the project, and also allows you to save this list to a file.

As you can see from the table, you had to remember many different commands and their arguments to fully create projects on Django. Now you only need to remember and type 1 command.

django-command

Now the routine task: creating migrations, applying migrations, updating locales looks like this.

django-command make_migrations migrate update_local
# или
django-command 4 7 2

Conclusion

The django-command tool is an effective way to increase the productivity of developing projects on the Django framework. It will help automate routine tasks, increase the overall efficiency of development and reduce the likelihood of errors.

I hope this article will help you save at least a little time when writing projects on the Django framework, and also give a quick start to beginners without having to spend hours searching for frequently used commands.

I would be glad to receive feedback. In the comments, write how you solve this problem. And what commands do you use in your Django projects.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *