Configure your Django project for multiple stages
Tags: django, python, deployment
Published: 2024-02-11 02:32:04

---
title: Configure your Django project for multiple stages
published: true
description: In this comprehensive guide, I walk you through the step-by-step process of setting up your Django project for various environments like local development, testing, and production.

tags: django, python, deployment
---

Hey guys, In the next article I want to provide you with a step by step guide on how to configure your Django project for multiple stages. Such as local, testing and production. 

## Django Settings. 

When a Django App is created, a file project.settings is generated. This file has all the settings for running your applications including the database settings, authentication settings, third party apps, etc. By default, Django calls this file in three places, manage.py, wsgi.py and asgi.py. The last two files are used to run your app in production, while the manage.py is the main file used for running operations e.g create apps, run migrations, and other django internal commands.

By default, Django calls the settings file in your project using python path syntax .e.g mysite.settings. We can create multiple files that we can use in different stages. 

In manage.py, wsgi.py and asgi.py uses the following line to tell which settings you want to use. Django calls the variable  DJANGO_SETTINGS_MODULE and takes the specified setting. If the variable doesn’t exist, It will use the one set by default.

```
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'exerciselogs.settings.dev')
```

## Create dev setting files.

First we are going to create a new directory called settings, where our setting files will reside.  Move the default settings file to this directory then rename it to base.py. You can use any descriptive name for this file. I use base because all our setting files will inherent from this file. Next create a new file called dev.py. 

In the first line of the file, add 

```
From .base  * 
```

Here It’s inheriting all the settings variables and configurations from base.py. Now we can overwrite the settings variables in dev.py. In base.py leave DEBUG=FALSE now in dev.py will 
Be DEBUG = True. dev.py will be the file that Django will call by default, as our development environment. 

In manage.py make some changes to the os.environ.setdefault statement in order to call dev.py. It should be something like this

```
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'exerciselogs.settings.dev')
```


When running python manage.py runserver you will see something like this:

```
Django version 4.2.4, using settings 'exerciselogs.settings.dev'
```


Here Django is telling us which setting file we are using.

If you access to a wrong URL in a browser you will see 

```
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
```

## Production Setting File.

Now we are going to configure the production setting file. 

Create a file called prod.py
Add inheritance from base.py  using the previous line code.
From base.py remove the DEBUG variable we are not going to need it here. 

In prod.py add

```
DEBUG = False.
```


In your computer create a environment variable called DJANGO_SETTINGS_MODULE and add the path syntax to prod.py


```
DJANGO_SETTINGS_MODULE=exerciselogs.settings.prod
```


When running manage.py, django will tell us that we are using the prod.py 



```
Django version 4.2.4, using settings 'exerciselogs.settings.prod'
```
Open the browser, search for a non existing route and It should show you the NOT FOUND page. This tells us that the DEBUG is set to False.

Here I explained how to create and prepare multiple configuration files in Django in order to have multiple configurations for different environments. I hope this article has been useful to improve the deployment of your application in Django. Any suggestion or doubt, you can write in the comments.