Skip to content

Tutorial How create an application with Django 1.6 on Openshift

Ivaylo Strandjev edited this page May 24, 2017 · 5 revisions

First all, you must have an account on Openshift and the rhc client tool installed on your computer.

You can create the account here

Once you have ruby installed, Install RHC client tool like so:

 $ sudo gem install rhc

Now we will prepare our environment on Openshift to deploy the polls application.

Create a python-2.7 application on Openshift.

 $ rhc app create -a mysite -t python-2.7

Notice your git remote address for later use.

Now add this upstream repo.

 $ mkdir mysite
 $ cd mysite
 $ git init
 $ git remote add upstream -m master git://github.com/rancavil/django-openshift-quickstart.git
 $ git pull -s recursive -X theirs upstream master

This actions create the next directory structure in your local machine.

 mysite/
     .gitignore
     .openshift/
         README.md
         action_hooks/  (Scripts for deploy the application)
             build
             post_deploy
             pre_build
             deploy
             secure_db.py
         cron/
         markers/
     setup.py   (Setup file with de dependencies and required libs)
     README.md
     libs/   (Adicional libraries)
     data/   (For not-externally exposed wsgi code)
     wsgi/   (Externally exposed wsgi goes)
         application (Script to execute the application on wsgi)
         openshift/  (Django project directory)
             __init__.py
             manage.py
             openshiftlibs.py
             settings.py
             urls.py
             views.py
             wsgi.py
             templates/
                 home/
                     home.html (Default home page, change it)
         static/ (Public static content gets served here)
             README

It is a lot of directories and files on your local machine...don't be afraid. We focus on the important stuff.

First edit the setup.py file located in mysite directory, and put your personal data to identify your application (this is optional).

 from setuptools import setup
 import os

 # Put here required packages
 packages = ['Django<=1.6',]

 # This is if you like use redis cloud w/Django...
 if 'REDISCLOUD_URL' in os.environ and 'REDISCLOUD_PORT' in os.environ and 
    'REDISCLOUD_PASSWORD' in os.environ:
      packages.append('django-redis-cache')
      packages.append('hiredis')

 setup(name='YourAppName',          # <= Put your application name, in this case 'mysite'
       version='1.0',
       description='OpenShift App', # <= Put your description if you want
       author='Your Name',          # <= Your name!!!!
       author_email='[email protected]',
       url='https://pypi.python.org/pypi',
       install_requires=packages,
 )

Save the changes.

Then you can make.

 $ git remote add openshift -f <openshift-git-repo-url> 
 $ git merge openshift/master -s recursive -X ours
 $ git push openshift HEAD

With the push, we "load" this directory structure on our Openshift account...You can check in your navigator.

 http://mysite-$yournamespace.rhcloud.com

When we make the push, all task are execute automatically for install and configure Django on Openshift. This executes $ python setup.py install on the remote Openshift for us.

Basically the task sequence is:

  1. Install Django 1.6 and all packages in install_require of the setup.py file.
  2. Execute the script deploy (file located in mysite/.openshift/action_hook)
  3. And execute the python script secure_db.py (file located in mysite/.openshift/action_hook) to create the password for the admin user.

See how the admin user name and password are created in [https://github.com/rancavil/django-openshift-quickstart#admin-user-name-and-password].

Login how admin with the password:

 http://mysite-$yournamespace.rhcloud.com/admin

Congrats, you has created your first Django application on Openshift...but what happen with the polls application?.

Well, we go to follow some topics of the tutorial of djangoproject.

Create a local development environment

We need to create a local environment for develop and test our application before upload it to OpenShift.

I recommend create a python virtaulenv.

Create a virtualenv and activate it:

 $ cd mysite
 $ virtualenv venv --no-site-packages
 $ source venv/bin/activate

Install Django 1.6 on our local machine.

In mysite/ directory, execute:

 $ python setup.py install

Create a local admin/password.

In the mysite/wsgi/openshift directory, execute:

 $ python manage.py changepassword admin
 $ Changing password for user 'admin'
 $ Password: 
 $ Password (again): 

This admin account and password are only for our local development enviroment.

Testing the install.

In the mysite/wsgi/openshift directory, execute:

 $ python manage.py runserver

Login with the admin user.

 http://localhost:8000/admin

Database setup

In this example, we'll use sqlite3 as databases. Don't worry for now.

Creating models

In your local machine, go to the mysite/wsgi/openshift directory, where manage.py file is and make.

 $ cd mysite/wsgi/openshift
 $ python manage.py startapp polls

That'll create a directory polls inside of mysite/wsgi/openshift, which is laid out like this.

 polls/
      __init__.py
      admin.py
      models.py
      tests.py
      views.py

This directory structure will house the poll application.

In our simple poll app, we will create two models: Poll and Choice. A Poll has a question and a publication date. A Choice has two fields: the text of the choice and a vote tally. Each Choice is associated with a Poll.

The concepts are represented by simple Python classes, Edit the polls/models.py file and write:

 from django.db import models

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

     def __unicode__(self):
          return self.question

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

     def __unicode__(self):
          return self.choice_text

Activating models

Edit the mysite/wsgi/openshift/settings.py file, and change the INSTALLED_APPS to include the string 'polls'. So it'll look like this.

 INSTALLED_APPS = (
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'polls',
 )

Make the poll app modifiable in the admin

Edit the mysite/wsgi/openshift/polls/admin.py and write:

 from django.contrib import admin
 from polls.models import Poll, Choice

 admin.site.register(Poll)
 admin.site.register(Choice)

This will register the models in the admin console.

Test locally before upload the changes to Openshift

In mysite/wsgi/openshift directory, execute:

 $ python manage.py syncdb
 $ python manage.py runserver

Login again like admin in our local develop environment. Check like user admin.

 http://localhost:8000/admin

Apply the changes on Openshift

You must be sure you are in the mysite/ directory.

And simply make:

 $ git add .
 $ git commit -am"activating the polls app on Django / Openshift"
 $ git push

This executes all tasks for create the polls application on Openshift.

Now we go to the admin web console on Openshift.

 http://mysite-$yournamespace.rhcloud.com/admin

Note: Remember you admin password for Openshift.

Again login to Openshift like admin user. We must see the models inside the admin console.

Resume

With this mini - tutorial, we have shown how to start developing an application with Django 1.6 on OpenShift.

You can follow the steps in the tutorial djangoproject.

I always recommend to test locally the changes, before uploading to the cloud (OpenShift). Try every change in our local team and then we're sure it got on OpenShift.