Django KFIMember Tutorial Part II - Creating Initial Application

 
Django online on Openshift!

Figure I-1 Our Django 1.6 application online on Openshift!

ARTICLE UPDATE : As I have unresolved problem when using Python 3.3 combined with Django 1.6 and PostgrerSQL 9.2 in Openshift, I dropped Python 3.3 requirement and rework this application using Python 2.7 instead.

Previously I had laid out the foundations of what Django application we are about to be build. Have a quick read again on what are those foundations. For now, let start with creating our application from scratch.

We will start by creating a Python 3.3 project on Openshift, prepare local Windows environment for it and then import it into a new Python Tools for Visual Studio (PTVS) project. Lastly, we will also prepare our local environment for Linux user, so that for those who prefer to use *nix environment will be able to follow along this tutorial series.

 

Installing Python 3.3

Very easy, just go to http://python.org/getit and download the appropriate distribution for your Operating System. Currently I am using Python 3.3 64-bit on Windows 7 64-bit. For complete instructions on how to properly install Python on Windows environment including its Virtualenv for Windows, you can read it on my other article here.

Preparing Openshift Django Application

If you haven't done so, make sure first to install RHC client tools which will making your life easier in maintaining Openshift application using complete CLI command. While I am using Windows to prepare this Openshift application, of course you are free to choose your own Operating System of choice.

To create this application, you can use Openshift Web Console, but here, let me show you how easy it is to use RHC command line to create an application:

1
rhc app-create kfimember python-3.3 postgresql-9.2

Create kfimember project in Openshift

Create kfimember project in Openshift using Windows Git Bash Shell

It will create a Python 3.3 application in Openshift and also automatically clone its private Git repository to the current directory. Although the application was cloned locally, you were still unable to run this application locally. For that, we must prepare our local environment with complete set of Python environment and its library. But the application itself is already available online on the internet using your domain name in Openshift. Mine is here http://kfimember-ekowibowo.rhcloud.com.

If you have wander around Openshift Web Console, you will notice that it did offer to automatically create a Django application using this Github repository. But at my latest test, it still use Django 1.4 + Python 2.6. Rather than hacking it to work with Django 1.6 + Python 3.3, let just create an empty Python 3.3 project and then we will install Django 1.6 there. It will gives us much more understanding to the overall Openshift architecture and Django in particular.

Creating Django 1.6 Project on Windows Environment

Let first have a look on directory structure created by our rhc client tools: 

Initial project layout for Python Openshift application

Initial project layout for Python Openshift application

Talking in Openshift way, we must create our Django application inside wsgi directory, using Django own django-admin.py script. To make things better, we will install Django 1.6 in this application own virtualenv directory, using this command:

1
virtualenv env

 

 
Create virtualenv

Create virtualenv

You can see that it use Python3.3 as the base of creating virtualenv. This is because I put C:\Python33 into my system path environment variable. Also inspect that I create env directory in the root directory of kfimember project. You can put this anywhere, really, as it will be included in .gitignore, so that it will not get stored into our precious Openshift repository. 

Now that we already have our virtualenv, let's activate it, install Django 1.6 in it and create our initial Django project: 

1
2
3
4
env\Scripts\activate.bat
pip install django==1.6
cd wsgi
django-admin.py startproject kfimember

One thing to note from the above commands, is that after a successful installation of Django 1.6, django-admin.py will be available to be executed directly as it is stored in env\Scripts  folder. Now, let see what is our current project layout after issuing django-admin.py script:

Django project layout

Django project layout

We can see our kfimember project already created beneath wsgi folder, and there is one module also named kfimember. You can recognize that it's a module, by the existence of __init__.py in that folder.

The entry point of all Django project management activities is its manage.py script. Using it, you can run local server, synch db and other managerial tasks. Let see whether we can run this Django application locally from within Windows:

1
2
C:\...\wsgi>cd kfimember
C:\...\wsgi\kfimember>python manage.py runserver 

If you see this screen, 

Django server ready to receive request!

Django server ready to receive request!

Congratulations! This mean you have successfully create local working Django application. Now you can test your Django application locally in http://localhost:8000.

Django runs on local installation

Django runs on local installation

Our next step is making sure that our Django application is also working in Openshift.

Commiting Changes to Openshift

Before committing any changes, make sure that you have a .gitignore file in your project root folder with the following contents:

1
2
3
4
5
6
7
8
*.py[co]
*.sw[po]
*.bak
*.orig
*~
.project
.pydevproject
env

It wil make sure that our env folder will not get stored in Openshift.

Although your Django application runs locally, it doesn't necessarily means that it will run in Openshift too. First, lets modify our setup.py file:

1
2
3
4
5
6
7
8
9
from setuptools import setup
 
setup(name='kfimember', version='1.0',
      description='KFI Member Application',
      author='Eko Wibowo', author_email='swdev.bali@gmail.com',
      url='http://www.python.org/sigs/distutils-sig/',
      install_requires=['django==1.6',
      ],
     )

We will start with one requirement only : Django 1.6.

Commit your code changes and push it to Openshift,

1
2
3
c:\..\kfimember>git add .
git commit -am'django runs locally'
git push origin master

 

If you expect your online Openshift version of your project to display the same output as your local version, well, don't get mad. It still display the initial welcome page. We must change our wsgi\application file that serve as the starting point of any Openshift WSGI application, to point to our newly installed Django application. It's not that trivial how to do it at first, but after having look at the changes in Python 3.3, I realize that the code to properly execute startup script in openshift is already there in \app.py. And after combining it with the required Django initialization script, I came up with this wsgi\application file that works in starting up our newly Installed Django application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python
import os, sys, imp
 
try:
   os.environ['DJANGO_SETTINGS_MODULE'] = 'kfimember.settings'
   sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'kfimember'))
 
   zvirtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'],
                           'virtenv', 'bin', 'activate_this.py')
   exec(compile(open(zvirtenv).read(), zvirtenv, 'exec'),
        dict(__file__ = zvirtenv) )
except IOError:
   pass
 
from django.core.handlers import wsgi
application = wsgi.WSGIHandler()

Commit and push your changes to Openshift, and you will see a warm welcoming page of Django, as seen in Figure 1 of this article.

Working with Python Tools for Visual Studio (PTVS)

Why PTVS? Well, in my earlier post, I had said that Windows is the most irritating development environment to work with. Got to say, currently I still stand by that statement Laughing.  But, let see what Microsoft had to offer with their Python Tools for Visual Studio and we are going to find out, whether working with Python development in Windows is still irritating as previously or whether PTVS able to persuade *nix users like us to install Windows, just so that we can use PTVS for Python development. 

PTVS Requirements & Installation

Let's begin with the unfortunate things : you can not use PTVS with Visual Studio Express. Bad news isn't it? This is because Visual Studio Express won't allow installation of Visual Studio Plug-Ins (which is what PTVS is all about). However, I've read that you can eventually use PTVS (along with Visual Studio IDE) even though without the existing installation of Visual Studio Pro, have a look on how to do it here! I haven't been able to test that myself, so I can't confirm it. My workstation currently installed with Visual Studio 2012 Pro, equipped with Python Tools for Visual Studio downloaded from here

Try to start VS.NET and create a Python Application (it will just print Hello World). Run it.

You will find that VS.NET able to use an existing Python interpreter already installed on your Windows environment. No big deal. But, at least we know that we can start developing Python application using VS.NET!

Import Our Existing Django Project

If you have installed PTVS properly, open firing up Visual Studio.NET, you will find that now it include Python in its list of available template. You can also saw that it even include Django Project there! But, as I test it, I realize that it creates project using an old Django 1.4 version. So, I switch my plan of introducing how to create a Django Project from scratch using PTVS, into preparing first Django 1.6 in Openshift, then importing it into a new Django project in PTVS.

Let start by importing our existing Django application, by selecting New Project in VS.NET and choose From Existing Python Code. Press OK.

Import existing Django project

 

 

Search for our wsgi\kfimember  folder for the the project root folder and Named it anything you like, but here I named it kfimember. Press Next.

Browse for root folder
 

 

Select your Python interpreter available in your system and choose startup script. As it's a Django project, you will have to choose manage.py for this. Press Next. 

Interpreter and startup script

 

In the final page, check Django so that VS.NET will treat this project with special care. Click Finish. 

Make sure to click Django here!
 

Finish? Not yet. Just one small step. If you have a look in Solution Explorer, you will see that it still use global Python 3.3 environment. As we already have our own virtualenv for this application, we should use it. Right click on the node Python Environment and choose Add Existing Virtual Environment.

 

Add existing virtual environment

 

And browse for our existing \kfimember\env folder. Now everything is set!

To run it, simply hit F5 which will debug our application using the default web browser installed on your system.

Congratulations! Now you have a fully functional PTVS ready to accompany you in developing Python application using Windows Environment.   

Conclusion

As usual, you can download code for Part II of this article here : kfimember-part-2.zip.
And test the application live in : http://kfimember-ekowibowo.rhcloud.com

Note that, I omitted how to prepare *nix to work with this Django application, as it turns out this article already way too long. I parted it into its own addendum article. 

Stay tuned for my next article! 





Leave comments

 authimage
  • Glad that it helps you ;)
    Enjoy!

    • eko
  • Thanks, Great article for the openshift setup!

    • clangkts

Copyright(c) 2014 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved