Sunday, April 13, 2008

Hello World Program for Google App Engine using Django

First at as Write a Hello World App without any frame work
Create a Python program called main.py in new directory helloworld

#!/usr/bin/env python
print
print ‘Hello, World!’

create a yami file named app.yaml

application: helloworld
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py

u are done. Run the server by typing the command

D:\workspace>dev_appserver helloworld/
INFO 2008-04-13 22:45:38,625 appcfg.py] Checking for updates to the SDK.
INFO 2008-04-13 22:45:53,640 appcfg.py] Update check failed: INFO2008-04-13 22:45:53,671
dev_appserver_main.py] Running application helloworld on
port 8080: http://localhost:8080

in your favoret browser type the url http://localhost:8080 to view the result






Now its keep going and create the helloWorld App using Django Web framework
First Create a django Project

D:\workspace\python>django-admin.py startproject helloworld

This command create a floder helloworld and some file get into the helloworld floder and create a django application

D:\workspace\python\helloworld>python manage.py startapp hello

This create command create creat a floder and some python program need for the application
Create application config file app.yaml

app.yaml

application: helloworld
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
script: main.py

Now create a python program named main.py this program will remain the same for all the django project running in Google App Engine.

main.py

# Google App Engine imports.
from google.appengine.ext.webapp import util

from django.core.management import setup_environ
import settings

setup_environ(settings)

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher


# Unregister the rollback event handler.
django.dispatch.dispatcher.disconnect(
django.db._rollback_on_exception,
django.core.signals.got_request_exception)

def main():
# Create a Django application for WSGI.
application = django.core.handlers.wsgi.WSGIHandler()

# Run the WSGI CGI handler with that application.
util.run_wsgi_app(application)

if __name__ == '__main__':
main()

just copy the code.

Now edit the urls.py file which is already created by the django-admin command

urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
# Example:
(r'^hello/', 'hello.views.current_datatime'),

# Uncomment this for admin:
(r'^admin/', include('django.contrib.admin.urls')),
)

Now edit the setting.py file edit the line which is underlined
# Django settings for helloGAppEngine project.
import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASE_ENGINE = '' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = '' # Or path to database file if using sqlite3.
DATABASE_USER = '' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.

# Local time zone for this installation. Choices can be found here:
# http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
# although not all variations may be possible on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
# http://blogs.law.harvard.edu/tech/stories/storyReader$15
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT.
# Example: "http://media.lawrence.com"
MEDIA_URL = ''

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = ')h)2wh3gj_xsjpn=v)fd0%!#p=j07@!-ey8sscnoe--&9=w2@l'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
#'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
)

ROOT_URLCONF = 'helloGAppEngine.urls'
ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
ROOT_PATH + '/templates',
)

INSTALLED_APPS = (
# 'django.contrib.auth',
'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.sites',
)

Now create a folder name templates and create html file

index.html



Python , Djanog and Google App Engine


Hello, World!

Now the time is {{ now }}



Now edit the view.py program in the hello folder

# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse
import datetime

def current_datatime(request):
now = datetime.datetime.now()
return render_to_response('index.html', {'now': now})

You are done

run the server

D:\workspace\python>dev_appserver helloworld/
INFO 2008-04-13 23:09:23,390 appcfg.py] Checking for updates to the SDK.INFO 2008-04-13 23:09:38,
390 appcfg.py] Update check failed: INFO 2008-04-13
23:09:38,421 dev_appserver_main.py] Running application helloworld on port 8080: http://localhost:8080

view the result in the browser.







1 comment:

dagmaryaeaton said...

Wynn & Encore casino in Las Vegas - Dr.CMC
Wynn & Encore casino. A 청주 출장샵 Las Vegas 충주 출장마사지 Strip boutique, they offer an extensive 원주 출장마사지 spa, a golf course, 광주광역 출장샵 casino, spa, a sauna and a nightclub. 논산 출장마사지