Learning Django with OpenShift

Background

The goal of this article is use the OpenShift Platform as a Service (PaaS) as a learning platform for Django. Most of the technical articles out there about running Django on OpenShift assume the user already understands how to administer Django environments and projects. This article is written from the perspective of someone who has done some python programming and wants to learn some Django without doing a bunch of setup work.

Since each OpenShift Gear “…is a container with a set of resources that allows users to run their applications”, a user can ssh in to test, troubleshoot, debug and learn. This turns out to be quite convenient for learning Django.

Django Quickstart

First, we must deploy and OpenShift application. The deployment is completely automated with the Django Quickstart. Once completed, the web interface will return all of the connection information necessary for Django, Git, and SSH. Estimate 5 minutes.

https://www.openshift.com/quickstarts/django -> Deploy Now

 

Screenshot-Django-Quickstart

Learning Environment

Once the application and framework are setup, it’s time to log in, do some basic configuration, and setup a developer workflow. This will allow us to focus on learning Django.

Use ssh to connect to the OpenShift Gear and setup a few things. Luckily, most of the work is done for you by OpenShift. Use the connection information provided for your Gear when the OpenShift application was created in the last step.

ssh [email protected]
Each OpenShift Gear has a small data directory where your application’s database is stored. Conveniently, this can also be used for scratch work. Clone your application’s git repo to your data directory. This is not recommended for a production application, but will allow you to start learning Django from a Mac, Windows, or Linux Desktop without worrying if the correct version of Django is installed locally.

git clone ~/git/django.git/
Configure a few environmental variables to make your workflow easier

cd ~/app-root/data/django/wsgi/openshift
echo "cd ~/app-root/data" >> ~/app-root/data/.bash_profile
source $OPENSHIFT_HOMEDIR/python-*/virtenv/bin/activate

Here are a couple of tests to verify that the Django environment is working correctly:

python -c "import django; print(django.get_version())"
python manage.py shell

Practice Workflow

Login from scratch and create a new Django Application to get comfortable with the workflow. These are the basic instructions to follow every time you connect to the learning environment. We are doing a couple of things here. First, we are creating the application with the Django admin utility, which creates a new directory. Then we are adding the directory to our git repository and committing the change. Finally, we are pushing the change, at which point, OpenShift will take over and perform all of the necessary steps to make your application live.

ssh [email protected]
source $OPENSHIFT_HOMEDIR/python-*/virtenv/bin/activate

python manage.py startapp mytest
git add mytest
git commit mytest -m "Created test application"
git push

Start Django Tutorial

The following six part Django Tutorial is great, but there are a couple of things to be aware of when working in an OpenShift environment. First, having your git repository checked out in the OpenShift Gear is only a good idea for a learning environment. Second, the portion of the tutorial called The development server will not work properly because the Django Quickstart configures and adds the OpenShift components necessary to start the Django application for you.

ssh [email protected]
source $OPENSHIFT_HOMEDIR/python-*/virtenv/bin/activate

Finally, turning debugging on can make running through the tutorial a lot easier

vi settings.py
Modify debug statement

if ON_OPENSHIFT:
DEBUG = True

Commit and push the changes

git commit settings.py -m "Turn on debugging"; git push
At this point, you can run through the tutorial, modifying the data model, and interacting with components in the shell, have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *