How to create a Django project from a template

Hello again! Especially for students of the course “Python Web Developer” 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 a popular Django template is django-cookiecutter. Django cookiecutter It has a wide range of functions and configurations that are already ready for self-sufficient deployment to production, this is the path that can be used to create almost any new project on Django.

Nonetheless, django-cookiecutter 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 name of the project).

Now you need to view each file in the project and replace all project_name on the {{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 variable {{project_name}} – this 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. Example you will find 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 doing django-admin startprojectwe will pass the flag –template for installation from our template. Suppose if you created a template in your home directory ~ / project-nameyou run:

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 an external folder and new_django_project for the project name).

Now you may wonder if it is possible to use a remote template from the repository on GitHub, for example. Oh sure!

How to create a Django project from a remote template

Flag –template for startproject can take 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 you need to Procfile. To pass Procfile through a template system so that {{project_name}} replaced by the actual project name, you can execute startproject with flag –name.

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

Make sure to Procfile there is 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, pay attention to ponee, a lightweight Django template, ready for use with Heroku.

Here is such a small but interesting material. Waiting for your comments and we invite you to a free webinarwithin which we We will learn in detail how to test projects on Flask, write and run tests for projects on Flask, and create test data using Faker and Factory Boy.

Similar Posts

Leave a Reply

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