3. Make changes and deploy them

Next, we’re going to install a new package, Django Axes, into the project (Django Axes keeps track of log-in attempts). Then we’ll test it and deploy it to the cloud.

Install a package

To be used in a containerised system, packages must be built into the image, otherwise the next time a container is launched, the package will not be there. The image is built by the Dockerfile, and in our Dockerfile for Django projects, this includes an instruction to process the project’s requirements.in file with Pip. This is where the package needs to be added. Open requirements.in and at the end of it add a new line:

django-axes==3.0.3

It’s important to pin dependencies to a particular version this way; it helps ensure that we don’t run into unwanted surprises if the package is updated, and the new version introduces an incompatibility.

Now you can build the project again by running:

docker-compose build

Configure the Django settings

Django Axes requires that it be listed in the project’s INSTALLED_APPS, in settings.py. Add axes to the list:

# all Django settings can be altered here

INSTALLED_APPS.extend([
    "axes",
])

A brief explanation of Aldryn Addons

This project uses the optional Aldryn Addons system, which makes it possible for projects to configure themselves. For example, you can can find all the configuration that Aldryn Django does for Django settings in addons/aldryn-django/aldryn_config.py. (Aldryn Django is simply a convenience wrapper for Django - the Django used by your project is a wholly standard Django installation obtained from PyPI.)

You don’t have to use Aldryn Addons on Divio; if you prefer to manage settings manually, that will work just as well. However it makes development much faster, as it takes care of all the settings that would otherwise need to be managed correctly for the different cloud environments as well as the local environment.

One advantage of Aldryn Django is that it declutters the settings.py file, removing deployment-related values that are better handled via environment variables, and also provides a guarantee that settings for database, media and so on will always be correct. Aldryn Django’s aldryn_config.py sets them appropriately for each environment, including the local development environment, and also appropriately at each stage of the build/deployment process.

In settings.py, you’ll find the lines:

import aldryn_addons.settings
aldryn_addons.settings.load(locals())

These lines load all those settings into the settings module. This includes populating INSTALLED_APPS. A good way to see what settings are applied is via Django’s diffsettings command:

docker-compose run web python manage.py diffsettings

Run migrations

Django Axes introduces new database tables, so we need to run migrations:

docker-compose run web python manage.py migrate

(As you have probably noticed, we can run all the usual Django management commands, but because we need to run them inside the containerised environment, we precede each one with docker-compose run web.)

Check the project

If you launch the project again with docker-compose up you’ll find Django Axes in the admin:

'Django Axes in the admin'

Test it by attempting to log in to the Django admin with an incorrect password.

Deploy to the cloud

If you are satisfied with your work, you can deploy it to the cloud.

We made changes to two files (requirements.in, settings.py). So:

git add .
git commit -m "Added Django Axes"
git push

On the project Dashboard, you will see that your new commit is listed as 1 Undeployed commit. You can deploy this using the Control Panel, or by running:

divio project deploy

When it has finished deploying, you should check the Test server to see that all is as expected. Once you’re satisfied that it works correctly, you can deploy the Live server too:

divio project deploy live

Using divio project push/pull

Your local database has new content, but your cloud database hasn’t been touched by the work you did locally. One very useful function of the Divio CLI is ability to push and pull your database and media storage to and from the cloud environments. For example, try:

divio project push db

This will push the local database to the cloud Test environment. Once the process has completed, you can refresh the cloud Test site; you’ll see that it now has the same content in its database as the local site.

Similarly, you can push/pull media files, and also specify which cloud environment. See the local commands cheatsheet. A common use-case is to pull live content into the development environment, so that you can test new development with real data.


The next section looks at some more complex configuration and application integration.