3. Add new applications to the project

Note

In this section, we’ll assume your project is already running (docker-compose up web) and that you are in your own shell, not at the bash prompt in a container.

3.1. Add a package to the project directory

The simplest way to add a new Django application to a project is by placing it in the project directory, so it’s on the Python path. We’ll use a version of the Polls application that’s part of the Django tutorial.

Download the application (tip: open a second terminal shell so you can leave the project running):

git clone [email protected]:divio/django-polls.git

Note

The URL above requires that you have provided your public key to GitHub.

Otherwise, use https://github.com/divio/django-polls.git.

And put the inner polls application directory at the root of your project (you can do this with: mv django-polls/polls .).

3.2. Configure the project

3.2.1. Configure settings

Edit settings.py to include the polls application:

INSTALLED_APPS.extend([
    "polls",
])

This settings.py already includes INSTALLED_APPS that have been configured by applications in the project - here we are simply extending it with new ones.

3.2.2. Configure URLs

Edit urls.py to add the URLconf for the polls application:

urlpatterns = [
    url(r'^polls/', include('polls.urls', namespace='polls')),
] + aldryn_addons.urls.patterns() + i18n_patterns(
    # add your own i18n patterns here
    *aldryn_addons.urls.i18n_patterns()  # MUST be the last entry!

3.3. Migrate the database

Run:

docker-compose run web python manage.py migrate

You will see the migrations being applied:

Running migrations:
  Rendering model states... DONE
  Applying polls.0001_initial... OK

And when that has completed, open the project again in your browser:

divio project open

You should see the new polls application in the admin:

The polls application appears in the admin

3.4. Deploy the project

3.4.1. Push your changes

If it works locally it should work on the Cloud, so let’s push the changes to the Test server and deploy there.

First, add the change:

git add settings.py urls.py polls

Commit them:

git commit -m "Added polls application"

And push to the Divio Cloud Git server:

git push origin develop

Note

The Control Panel will display your undeployed commits, and even a diff for each one.

3.4.2. Deploy the Test server

divio project deploy test

And check the site on the Test server:

divio project test

Optionally, if you made some local changes to the database (perhaps you added some polls), you can push the database to the local server too, with:

divio project push db

(You’ll need to redeploy to see the results.)

3.5. Add a package via pip

Often, you want to add a reusable, pip-installable application. For this example, we’ll use Django Axes, a simple package that keeps access logs (and failed login attempts) for a site.

3.5.1. Add the package

Add django-axes==2.3.2 (it’s always sensible to specify a version number in requirements) to the project’s requirements.in:

# <INSTALLED_ADDONS>  # Warning: text inside the INSTALLED_ADDONS tags is auto-generated. Manual changes will be overwritten.
[...]
# </INSTALLED_ADDONS>
django-axes==2.3.2

(Make sure that it’s outside the automatically generated # <INSTALLED_ADDONS> section.)

3.5.2. Rebuild the project

The project now needs to be rebuilt, so that Django Axes is installed:

docker-compose build web

3.5.3. Configure settings

In the settings.py, add axes to INSTALLED_APPS:

INSTALLED_APPS.extend([
    "polls",
    "axes",
])

(Note that this application doesn’t need an entry in urls.py, because it only uses the admin).

3.5.4. Run migrations

Now the database needs to be migrated once again for the new application:

docker-compose run web python manage.py migrate

Check that it has installed as expected (Django Axes will show its records in the admin).

3.5.5. Deploy to the Cloud

To deploy this to the Test server, push your changes, and deploy once more:

git add settings.py requirements.in
git commit -m "Added Django Axes"
git push origin develop
divio project deploy test