Friday, June 29, 2012

Journey's in Python/Django - Hello World Part 3

In the previous stages I have outlined the environment I am setting out to create for a starting point for Python/Django applications. I plan to post the default configuration to github. Before we get there we need to get a working basic application.

So far we have a python server that runs on our local machine but it does nothing. So let's create a home page.

First I want to create an app folder to holds the code that supports the home page. To do that we can create a new app folder: home

In an earlier step I had created an apps folder. This is intended to hold the various apps or modules we create.

Let's switch in to that folder. So starting from the helloworld folder that contains the file: 

cd apps

We can use a built in command to create the basic files that comprise an app. We will call our app "home"

python ../ startapp home

This creates the following files in the "Home" folder under apps:

Okay, so my first problem is that I have created an empty database file in db/db.db but when I do runserver Django tells me to make sure I set the information in the DATABASES setting. 

To work out what is going on I edit my and add a simple pair of print statements and re-run the server:

print "Base:",BASE_DIR
print "Database:",DBPATH

The console tells me the base folder is a third level of "helloworld". Yes - I agree - very confusing. Let's fix that.

I switch to the top level helloworld folder which contains:


This is where my VirtualEnv is configured so I am going to rename the folder helloworld_env and add a readme.txt file to remind me to activate the VirtualEnv using 

source bin/activate

After trying to re-run the server things are not working. Time for a re-think. I believe VirtualEnv doesn't like the folder renaming. 

So I decided to go to my PycharmProjects folder where I keep all my Python projects.  I create a virtualenv folder

mkdir virtualenv
cd virtualenv

I then re-create the helloworld application folder inside the virtualenv folder. 

virtualenv hw
cd hw
source bin/activate
pip install django

I copy the helloworld application structure in to the hw folder cd to the folder and runserver and we are back in business.

Now my folder structure reminds me I should be running a virtualenv.

Now my database file is still pointing to 2 levels of helloworld folder. That is still a little confusing. Time to rationalize the folder structure a little more.

The second level helloworld folder has these files:

I decide to move these to the parent folder (the top level helloworld folder) and remove the helloworld sub-folder.

Trying to run the server and I get error message. Problems finding the settings file. I obviously need to make some configuration changes.

Change:    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "helloworld.settings")
To:           os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

Now it is not finding the helloworld.wsgi module.

Change:  WSGI_APPLICATION = 'helloworld.wsgi.application'
To: WSGI_APPLICATION = 'wsgi.application'

Change:  ROOT_URLCONF = 'helloworld.urls'
To: ROOT_URLCONF = 'urls'

Back in business!

Quit the server and run

python syncdb

This creates the database.

Now let's get the django admin working.

In settings enable the admin portal lines in the INSTALLED_APPS section:

    # Uncomment the next line to enable the admin:

Add the following line after the STATIC_URL entry:

ADMIN_MEDIA_PREFIX = '/static/admin'

In make the following changes:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(,

Now going to localhost:8000/admin should prompt you to enter a user id and password (the one you created when you ran the syncdb command.

We now have access to the administration tools.

We have accomplished quite a bit here. In the next post in this series I will actually look at getting the home page up and running.

Posted via email from ekivemark: pre-blogspot