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 thebash 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 itin the project directory, so it’s on the Python path. We’ll use a version ofthe Polls application that’s part ofthe Django tutorial.

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

  1. git clone [[email protected]](http://docs.divio.com/cdn-cgi/l/email-protection):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:

  1. INSTALLED_APPS.extend([
  2. "polls",
  3. ])

This settings.py already includes INSTALLED_APPS that have beenconfigured by applications in the project - here we are simply extending itwith new ones.

3.2.2. Configure URLs¶

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

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

3.3. Migrate the database¶

Run:

  1. docker-compose run web python manage.py migrate

You will see the migrations being applied:

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

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

  1. 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 tothe Test server and deploy there.

First, add the change:

  1. git add settings.py urls.py polls

Commit them:

  1. git commit -m "Added polls application"

And push to the Divio Cloud Git server:

  1. git push origin develop

Note

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

3.4.2. Deploy the Test server¶

  1. divio project deploy test

And check the site on the Test server:

  1. divio project test

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

  1. 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 thisexample, 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 inrequirements) to the project’s requirements.in:

  1. # <INSTALLED_ADDONS> # Warning: text inside the INSTALLED_ADDONS tags is auto-generated. Manual changes will be overwritten.
  2. [...]
  3. # </INSTALLED_ADDONS>
  4. 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:

  1. docker-compose build web

3.5.3. Configure settings¶

In the settings.py, add axes to INSTALLED_APPS:

  1. INSTALLED_APPS.extend([
  2. "polls",
  3. "axes",
  4. ])

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

3.5.4. Run migrations¶

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

  1. docker-compose run web python manage.py migrate

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

3.5.5. Deploy to the Cloud¶

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

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

原文: http://docs.divio.com/en/latest/introduction/03-add-applications.html