Automate the Boring Stuff of Starting a Django Project

Run a script that sets the project the way you need

Fabrício Barbacena
Towards Data Science

--

Photo by Diego PH on Unsplash

INTRODUCTION

This article’s title is an obvious tribute to Al Sweigart’s book Automate the Boring Stuff with Python, which I always recommend to people who want to start learning Python the right way. Although Al’s book doesn’t cover Django, it gives the readers a good understanding of how Python is a great tool to automate boring, repetitive daily tasks.

Some of you, after reading one or more of my tutorials, might already know that I am a big Django fan. But we must admit that, when starting a new Django project, we need to run a lot of commands on the terminal, create some folders and files manually, and make recurrent changes on some other ones. And most of these initial tasks are repetitive, which make them a great opportunity for automation.

So, I wrote this Python script that allows me to create a new Django project the way I need it to be before I start working its code. I preferred this approach to the other one, that is, copying or cloning a starter Django project. In fact, I think the script gives me more flexibility to customize the initial project structure the way I need, by creating a project with two, five, ten or one hundred apps at once, for example, if I want to.

(A quick note here: please don’t create one hundred apps on your Django project. Just because you can, it doesn’t mean you should).

I won’t discuss my code line by line, since you can consult the README.md documentation and the comments on my_django_starter.py for the details. I will just make some remarks on the problems I wanted to solve by writing this automation script.

MY PERFECT DJANGO STARTER PROJECT FEATURES

1. It already contains all the new apps folders I will need for my work, with the following characteristics:

  • a APP_NAME/templates/APP_NAME folder;
  • a urls.py file, since I like to have my app routes handled by a specific urls.py file, inside the app folder;
  • code inside urls.py and views.py enough to make the route http://localhost:8000/APP_NAME work.

2. The terminal commands below have already been executed:

django-admin startproject PROJECT_NAME .python manage.py startapp APP_NAME # for each apppython manage.py migratepython manage.py createsuperuser

3. These extra folders are created and ready to be used:

/media
/scripts
/templates
/templates/static

4. Modifications in the PROJECT_NAME/settings.py file:

  • the django_extensions package and all my apps names are already included automatically in the INSTALLED_APPS list;
  • the code for static, media and templates directories is set;
  • initial multiline comment is already removed (the same to the one in urls.py)

5. Changes in the PROJECT_NAME/urls.py file, so that the apps urls.py files are already included and its initial routes can be navigated.

This is all achieved by following a principle that I already had explored on my article on How to convert a Python Jupyter notebook into an RMarkdown file: .py and .html files are plain text files and thus they can be read, edited, and saved by our Python code as strings.

FINAL REMARKS

We could make even further automation code for a starter Django project, such as:

  • Create templates files with {% extends 'base.html' %} and {% block content %} tags;
  • Make some other files and directories available from the beginning;
  • Add code to change the LANGUAGE and TIMEZONE automatically, from the value we set in the config_info part of my_django_starter.py;
  • If we are making CRUD apps with Class-based Views, we can already populate the templates/APP_NAME folders with the .html files structured with the names demanded by the Class Views ( APP_NAME_list.html, APP_NAME_form.html, APP_NAME_detail.html and so on) , and we could also give them some basic content;

Now it is your turn: create your own automation script to start your new Django projects the way you need. Just don’t keep typing the same commands over and over again when you don’t have to. You are a programmer: write a program that makes this work for you instead.

If you want to read more about Django, check the articles linked below.

I thank you again, dear reader, for having honored my text with your time and attention.

Happy coding!

--

--

Python and Django Developer • Data Analyst • BI Consultant • Data Science • Data Engineering • https://linktr.ee/fabriciobarbacena