Prepare your application for deployment
Set up your application for production by updating some of the most common settings.
Add libraries
You'll use two new libraries for your project:
whitenoise
to serve static filespsycopg2-binary
to connect to PostgreSQL, the production database
Install those libraries in your project:
In Visual Studio Code, open requirements.txt.
Add the following code to the end of requirements.txt.
whitenoise psycopg2-binary
Open a new terminal pane by selecting Terminal > New terminal.
Install the libraries by running the following command.
pip install -r requirements.txt
Create a production settings file
The values you assign to two core settings, ALLOWED_HOSTS
and DATABASES
, depend on the environment that hosts the application. The default settings are designed for development. To run your app in production, ensure these settings are updated properly.
ALLOWED_HOSTS
controls the servers that are allowed to host or run your application. You'll configure it to allow the site to run from Azure and locally. DATABASES
contains the list of available connection strings.
A common way to configure the settings is to create a second Python file that contains the collection of settings for production. Then a check at the end of the settings.py determines whether to use the production settings.
Now you'll create a production settings file and add the check to determine if your application is running in production:
Create a new file inside project. Name it azure.py.
Add the following code to import
os
.from .settings import * import os
Add the following code to the end of the file to override
ALLOWED_HOSTS
to allow Azure to host the application and to define trusted origins.ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else [] CSRF_TRUSTED_ORIGINS = ['https://'+ os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
Note
Azure App Service automatically creates an environmental variable named
WEBSITE_HOSTNAME
. This variable contains the URL for your website. You can use this variable to determine whether your application is running on Azure.Add the following code to configure the database connection string for PostgreSQL.
hostname = os.environ['DBHOST'] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': os.environ['DBNAME'], 'HOST': hostname + ".postgres.database.azure.com", 'USER': os.environ['DBUSER'], 'PASSWORD': os.environ['DBPASS'] } }
Note
You'll set the environmental variables on Azure in a later step.
Note
The database connection is for PostgreSQL Flexible Server. For PostgreSQL Single Server, set the
USER
value toos.environ['DBUSER'] + "@" + hostname
.The connection string you're using is for PostgreSQL. You provide the following information:
- ENGINE: Database type
- NAME: Name of the database
- HOST: URL for the server
- USER: Username to use to connect to the database
- PASSWORD: Password for the user
Add the following code to the bottom of the file to enable
whitenoise
, which will serve static files.MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', # Enables whitenoise for serving static files 'whitenoise.middleware.WhiteNoiseMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
WhiteNoise is middleware that reads user requests for static files such as CSS or JavaScript. It also ensures the files are served correctly. You registered the middleware by updating the
MIDDLEWARE
array. You registered aSTATIC_ROOT
to store static files.Set the
SECRET_KEY
with one read from environmental variables by adding the following code.SECRET_KEY = os.getenv('SECRET_KEY')
You'll create a new secret key after you deploy the application and store it as an application setting.
Disable debugging mode by adding the following code.
DEBUG = False
Configure your app to use the production settings file
Now that you've created the production settings file, you can update your application to load the file in production. Start by looking for the WEBSITE_HOSTNAME
environmental variable. This variable indicates the application is running on Azure.
Open settings.py.
Add the following code to the end of the file to override the necessary settings when the app runs in production.
import os if 'WEBSITE_HOSTNAME' in os.environ: # Running on Azure from .azure import *
Save all files by selecting File > Save all.
You've now configured a Django application for production.