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):
- 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:
- INSTALLED_APPS.extend([
- "polls",
- ])
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:
- 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:
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:
- 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 difffor 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 addedsome 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 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
:
- # <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 #
section.)
<INSTALLED_ADDONS>
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 itonly 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 inthe 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
原文: http://docs.divio.com/en/latest/introduction/03-add-applications.html