Skip to content

Update the Django documentation #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions Guides/Python/Django.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ example app specifies [Django][django], [MySQL driver][mysql-driver] and
looks like this:

~~~
Django==1.7.1
gunicorn==19.1.1
Django==1.8.3
gunicorn==19.3
MySQL-python==1.2.5
~~~

Expand Down Expand Up @@ -60,13 +60,49 @@ Left from the colon we specified the **required** process type called `web`
followed by the command that starts the app and listens on the port specified
by the environment variable `$PORT`.

### The Actual Application Code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to explain the Django specifics here. This is the official Django tutorial app. You can link to the official tutorial there for details. Simply state that it's the same and looks something like this, followed by the code example.


The actual application code is taken from the [official Django tutorial](https://docs.djangoproject.com/en/1.8/intro/tutorial01/) and explains in detail how the applications works.

~~~python
import datetime

from django.db import models
from django.utils import timezone


class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __unicode__(self):
return self.question

def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'


class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()

def __unicode__(self):
return self.choice
~~~

### Production Database

The original tutorial application uses SQLite as the database in all
environments, even the production one. It is not possible to use a SQLite
database on cloudControl because the filesystem is
[not persistent][filesystem]. To use a database, you should choose an Add-on
from [the Data Storage category][data-storage-addons].
from [the Data Storage category][data-storage-addons] after creating and pushing
the application to cloudcontrol.

In this tutorial we use the [Shared MySQL Add-on][mysqls]. Have a look at
`mysite/settings.py` so you can find out how to
Expand Down Expand Up @@ -109,7 +145,7 @@ DATABASES = {
## Pushing and Deploying your App

Choose a unique name to replace the `APP_NAME` placeholder for your
application and create it on the cloudControl platform:
application and create it on the cloudControl platform:

~~~bash
$ cctrlapp APP_NAME create python
Expand All @@ -119,36 +155,41 @@ Push your code to the application's repository, which triggers the deployment im

~~~bash
$ cctrlapp APP_NAME push
Counting objects: 49, done.
Counting objects: 53, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (33/33), done.
Writing objects: 100% (49/49), 8.80 KiB | 0 bytes/s, done.
Total 49 (delta 11), reused 38 (delta 8)
Compressing objects: 100% (44/44), done.
Writing objects: 100% (53/53), 9.33 KiB | 0 bytes/s, done.
Total 53 (delta 12), reused 0 (delta 0)

-----> Receiving push
-----> No runtime.txt provided; assuming python-2.7.3.
-----> Preparing Python runtime (python-2.7.3)
-----> No runtime.txt provided; assuming python-2.7.8.
-----> Preparing Python runtime (python-2.7.8)
-----> Installing Distribute (0.6.36)
-----> Installing Pip (1.3.1)
-----> Installing dependencies using Pip (1.3.1)
Downloading/unpacking Django==1.7.1 (from -r requirements.txt (line 1))
Running setup.py egg_info for package Django
Downloading/unpacking Django==1.8.3 (from -r requirements.txt (line 1))
...
-----> Building image
-----> Uploading image (29.9 MB)
Successfully installed Django gunicorn MySQL-python
Cleaning up...

-----> Building image
-----> Uploading image (28.8 MB)

To ssh://[email protected]/repository.git
* [new branch] master -> master
* [new branch] master -> master
~~~

Add MySQLs Add-on with `free` plan to your deployment and deploy it:
Add MySQLs Add-on with 'free' plan to your deployment and deploy it:

~~~bash
$ cctrlapp APP_NAME addon.add mysqls.free
$ cctrlapp APP_NAME deploy
~~~

## Migrating the database

Finally, prepare the database using the
[Run command][ssh-session] (when prompted create admin user):
[Run command][ssh-session] (when prompted you can create an admin user):

~~~bash
$ cctrlapp APP_NAME run "python manage.py syncdb"
Expand Down