Creating a Boilerplate for new Django Project

dipesh
5 min readJun 28, 2020
Photo by Scott Graham on Unsplash

If you are familiar with the default folder structure of a new Django project then this article will be easier to understand. This article will not cover the basics of the project folder structure in the Django project.

During my Django development experience, I’ve faced a scenario where I have to go along with the same repetitive work to tweak the default project structure of a new Django project. At that moment, I decided to create a GitHub repository which will modify the default structure and from then I can use that repository to create a new project.

Let’s get started by creating the main root directory my_django_project
The folder structure will be:

creating main root directory named 'my_django_project'
The folder structure will be:
my_django_project
|
|---assets (folder created once static files are collected)
|
|---django_project (our boilerplate project)
|
|---media (folder created once items were uploaded)
|
|---db.sqlite3
|
|---venv (our virtual environment
  1. Create virtual environment, activate it, and follow the other steps under the activated virtual environment.
  2. Install django
  3. Create a new Django project named django_project
  4. Create a custom app core
$ cd ~/Desktop # your preferred working directory
$ mkdir my_django_project && cd my_django_project
$ python3 -m venv venv
$ source venv/bin/activate #activating the virtual environment
$ pip install django
$ django-admin startproject my_project
$ cd django_project
$ django-admin startapp core #custom user app
# update your requirements.txt

5. Update the settings file

Instead of keeping all settings inside a settings.py file, we will break down it into a package setting inside config and separate security concern settings in env.py.

We will add env.py in .gitignore so that all the security concerns will not display in version control. We will keep a sample of how env.py should look like in a file named env.example.py(we will keep this in our version control).

A fresh look into how settings will be changed from simple settings.py to a package (__init__.py is not shown in the tree structure).|django_project
|
|---config
| | |---settings
| | | |---base.py
| | | |---env.py (ignored by git)
| | | |---env.example.py
| | |
| | |---asgi.py
| | |---urls.py
| | |---wsgi.py

While moving our settings.py to settings package inside. Update base.py as follows:

django_project/config/settings/base.py

Don’t forget to create env.py and env.example.py. And add the following snippets in env.py and env.example.py and update your env.py with a secure configuration.

django_project/config/settings/env.py

Also, we need to make an update on our asgi.py, wsgi.py and manage.py.

django_project/manage.py
django_project/config/wsgi.py
django_project/config/asgi.py

6. Create static_files directory, from where django-admin collectstatic will collect the static files.

|---django_project
| | | ...
| | | ...
| | |static_files (all static files we use in development)
| | |---base (project as a whole specific static files)
| | | |---css
| | | |---img
| | | |---js
| | |---core (my custom app specific)
| | | |---css
| | | |---img
| | | |---js
| | | ...
| | | ...

7. Add templates directory as follows:

|---django_project
| | | ...
| | | ...
| |---templates
| | |---core (custom app specific)
| | |---<other app specific templates>
| | |---base.html (included basics of jquery and bootstrap)
| | | ...
| | | ...

Here base.html will look like:

django_project/templates/base.html

8. Now it’s time to create a package for the wrapper of our custom apps. We will call this wrapper package as apps and move our initial app core inside it. And we will create urls.py inside it (which will be pointed by the config.urls and this URL file will include all the app-specific urls.py.

|---django_project
| | | ...
| | | ...
| |---apps
| | |---core (custom app)
| | |---...<other apps>...
| | |---urls.py
| | | ...
| | | ...

Every time we create a new app, we will move it inside the apps package.

9. Add some home template, home views in our core app and add some files and update some existing files core.urls, apps.urls, and config.urls:

django_project/templates/core/index.html
django_project/apss/core/views.py
django_project/apps/core/urls.py
django_project/apps/urls.py
django_project/config/urls.py

10. And at last, let's add a command which can be used to rename our django_project name as our choice.

django_project/apps/core/management/commands/rename_project.py

11. To publish this project in our GitHub repo, we have to initialize git from our my_django_project/django_project/ directory. And When we clone this project we have to specify a special directory for the project and can clone the project in that directory. This is to make sure that our project looks cleaner, beautiful, and less messed up with all the media files, collected static files, virtual environment, and also the SQLite database (in case we used SQLite).

Now our first Boilerplate code is ready for a new Django project. Overall folder structure used in this architecture is as follows:

my_django_project
|
|---assets (folder created once static files are collected)
|
|---django_project
| |---apps
| | |---core (custom app)
| | |---...<other apps>...
| | |---urls.py
| |
| |---config
| | |---settings
| | | |---base.py
| | | |---env.py (ignored by git)
| | | |---env.example.py
| | |
| | |---asgi.py
| | |---urls.py
| | |---wsgi.py
| | |
| | |static_files (all static files we use in development)
| | |---base (project as a whole specific static files)
| | | |---css
| | | |---img
| | | |---js
| | |---core (my custom app specific)
| | | |---css
| | | |---img
| | | |---js
| | |
| |---templates
| | |---core (custom app specific)
| | |---<other app specific templates>
| | |---base.py (included basics of jquery and bootstrap)
| |
| |---.gitignore
| |---manage.py
| |---requirements.txt
|
|---media (folder created once items were uploaded)
|---db.sqlite3

There are no specific rules to follow how the boilerplate project should be written. After all, it is written in such a way that it will reduce the initial setup of the project.

The way I modified the project structure may not be satisfactory. I used this repository to make my initial Django development fast. You can modify the initial Django project architecture in your own way.

If you want to have a look into all the codes I had written to create my own boilerplate Django project then you can visit my GitHub repo. The project I mentioned in the link will also include the integration of django debug toolbar.

Any suggestions regarding this architecture are highly appreciated. Thank You.

--

--