How to create a Django project from a template

Hello again! Especially for students of the course "Web-developer in Python", we have prepared another interesting translation.





A Django project template is a natural way to solve problems that arise when the default Django project format no longer meets the requirements. Today in this guide you will learn how to create your own project from a template.

What is a project template in Django?


One of the little-known features of Django is the ability to create a project from a template, that is, a custom directory structure.

This is convenient when the default project format is no longer enough (and it will definitely not be enough when you want to deploy your solution on production, believe me), or when you reach the point where you will use the same configurations again and again for lots of projects on Django.

A Django project template is nothing more than a project with a predefined custom directory structure that you can create yourself in minutes.
An example of the popular Django template is the django-cookiecutter . Django cookiecutterIt has a wide range of functions and configurations that are ready for self-sufficient deployment to production, this is the path that can be used to create almost any new project on Django.

However, the django-cookiecutter is sometimes redundant, especially for beginners, so before using it, create a simpler Django template that can better help you understand the basics.

In the following sections, you will learn how to create your own template.

How to create your own Django project template


To get started, create a Python virtual environment in the selected folder and install Django (note that these are four separate commands):

mkdir project-name && cd $_
python3 -m venv venv
source venv/bin/activate
pip install django

Then create a Django project with:

django-admin startproject project_name .

Pay attention to the name, it should be exactly project_name . After that, open the project in the editor.

(Note that in our example, I used project_name for the folder and for the project name).

Now you need to look at each file in the project and replace all project_name with {{project_name}} (a single-line sed command is fine).

Files you will need to modify:

  • manage.py
  • settings.py
  • wsgi.py
  • asgi.py

As you might have guessed, the {{project_name}} variable is a placeholder. For example, in manage.py you will need to replace the following:

# manage.py

def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
    try:
# omit

On this:

# manage.py

def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')
    try:
# omit

In wsgi.py you change this:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')

To the following:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')

Etc.

In addition, you can create any configuration you want, or even better, parse the Django settings into separate files for development, testing and production. You will find an example here .

Before proceeding to the next step, be sure to remove the virtual environment from the project template.

How to create a Django project from your own template


Now that you have the project template, you can use it.

To do this, create a new Python virtual environment in another folder and install Django:

mkdir new-django-project && cd $_
python3 -m venv venv
source venv/bin/activate
pip install django

Now, instead of just executing django-admin startproject , we will pass the --template flag to install from our template. Suppose if you created a template in your ~ / project-name home directory , you would do:

django-admin startproject --template ~/project-name new_django_project .

This command will create a new Django project from the template.

(Note that in this example, I used new-django-project as the external folder and new_django_project for the project name).

Now you may wonder whether it is possible to use a remote template from the repository on GitHub, for example. Yes of course!

How to create a Django project from a remote template


The --template flag for startproject can accept a URL as a remote source. So you can create a Django project from a remote template, for example:

django-admin startproject --template https://github.com/username/repo/archive/master.zip new_django_project .

Bonus: templates for everything


In addition to standardizing files related to Django, you can do the same for any other file in the project.

Imagine you need to deploy to Heroku where a Procfile is required . To pass Procfile through the template system so that {{project_name}} is replaced with the actual name of the project, you can run startproject with the --name flag .

django-admin startproject --template https://github.com/username/repo/archive/master.zip --name=Procfile new_django_project .

Make sure that Procfile has a placeholder for project_name :

release: python manage.py migrate
web: gunicorn -w 3 {{ project_name }}.wsgi --log-file -

Sources


To create a sophisticated Django template, take a look at ponee , a lightweight Django template, ready for use with Heroku.

Here is such a small but interesting material. We are waiting for your comments and are invited to a free webinar , in the framework of which we will learn in detail how to test projects on Flask, write and run tests for projects on Flask, create test data using Faker and Factory Boy.

All Articles