Code formatting in Django

Augustine Joseph
2 min readJul 29, 2023

--

Code formatting in Django using Black and remove-print-statements.

Removing print statements

To remove print from django in one step, programs like remove-print-statements can be used.

To view the list of print statements that will be removed. Run the following code.
The program will not automatically crawl inside each sub directory and removes the print statements. To make it do so, the following code is used.

git ls-files -- '*.py' | xargs remove-print-statements --dry-run --verbose *.py

The output of the above dry run will be a list of print statements that will be removed.

list of comments to be removed.

To remove the print statements from the code, run the following code.

git ls-files -- '*.py' | xargs remove-print-statements  

Now all the print statements in all the modules inside the entire project is removed.

Formatting code using Black

Black is a code formatter that formats the Python code into PEP8 standard.
To install black:

pip install black

To run black, go to the projects base directory and run:

black .

Here the . means all the folders and files inside the project.

--

--

No responses yet