-
 KDE-Apps.org Applications for the KDE-Desktop 
 GTK-Apps.org Applications using the GTK Toolkit 
 GnomeFiles.org Applications for GNOME 
 MeeGo-Central.org Applications for MeeGo 
 CLI-Apps.org Command Line Applications 
 Qt-Apps.org Free Qt Applications 
 Qt-Prop.org Proprietary Qt Applications 
 Maemo-Apps.org Applications for the Maemo Plattform 
 Java-Apps.org Free Java Applications 
 eyeOS-Apps.org Free eyeOS Applications 
 Wine-Apps.org Wine Applications 
 Server-Apps.org Server Applications 
 apps.ownCloud.com ownCloud Applications 
--
-
 KDE-Look.org Artwork for the KDE-Desktop 
 GNOME-Look.org Artwork for the GNOME-Desktop 
 Xfce-Look.org Artwork for the Xfce-Desktop 
 Box-Look.org Artwork for your Windowmanager 
 E17-Stuff.org Artwork for Enlightenment 
 Beryl-Themes.org Artwork for the Beryl Windowmanager 
 Compiz-Themes.org Artwork for the Compiz Windowmanager 
 EDE-Look.org Themes for your EDE Desktop 
--
-
 Debian-Art.org Stuff for Debian 
 Gentoo-Art.org Artwork for Gentoo Linux 
 SUSE-Art.org Artwork for openSUSE 
 Ubuntu-Art.org Artwork for Ubuntu 
 Kubuntu-Art.org Artwork for Kubuntu 
 LinuxMint-Art.org Artwork for Linux Mint 
 Arch-Stuff.org Art And Stuff for Arch Linux 
 Frugalware-Art.org Themes for Frugalware 
 Fedora-Art.org Artwork for Fedora Linux 
 Mandriva-Art.org Artwork for Mandriva Linux 
--
-
 KDE-Files.org Files for KDE Applications 
 OpenTemplate.org Documents for OpenOffice.org
 GIMPStuff.org Files for GIMP
 InkscapeStuff.org Files for Inkscape
 ScribusStuff.org Files for Scribus
 BlenderStuff.org Textures and Objects for Blender
 VLC-Addons.org Themes and Extensions for VLC
--
-
 KDE-Help.org Support for your KDE Desktop 
 GNOME-Help.org Support for your GNOME Desktop 
 Xfce-Help.org Support for your Xfce Desktop 
--
openDesktop.orgopenDesktop.org:   Applications   Artwork   Linux Distributions   Documents    Linux42.org    OpenSkillz.com   
 
Home
Apps
Artwork
News
Groups
Knowledge
Events
Forum
People
Jobs
Register
Login

-
- News . 
0
votes
click to vote up

Writing your first Django web app with Google App Engine, part 1


Published Jun 7 2013 via RSS

I’ve decided to use Google’s App Engine to write my Remember 4 Me web apps. It allows for Python 2.7 app development using Google’s SDK. I used the Django Tutorial and used the App Engine model instead of the the Django model. I coded this on my MacBook so the instructions will be for the Mac. However, you can do the same thing on Linux or Windows.

Setting up App Engine

First, log into the Google App Engine Console and click Create Application. Fill out the following form:

image

The application identifier rem4me-me-pollapp is taken, so you’re going to have to pick one for yourself. Also, the tutorial doesn’t use authentication, so leaving the default is okay. Once you’ve picked a unique application identifier and given it a title, click the Create Application button to finish the process.

Python 2.7

The Google App Engine uses Python 2.7 as its programming framework. From a Terminal window, type python to see what version you’re running.

Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

As you can see, Mac’s come with Python 2.7 already installed. If you want the latest 2.7.5 version of Python or if your operating system doesn’t have Python 2.7, download Python and install it for your operating system.

Google App Engine SDK for Python

You need to download and install the Google App Engine SDK for Python. On the Mac, this places the SDK in /usr/local/google_appengine. Be sure your install creates the proper symlinks for app development.

Django 1.5

The Google App Engine SDK comes with Django 1.5. So there’s no need to download it from Django’s website. It is installed in /usr/local/google_appengine/lib/django-1.5 and can be accessed there. There are some things you need to setup in your ~/.profile first. So, type vi ~/.profile and add

# Set PYTHONPATH for Google App Engine's Django 1.5
PYTHONPATH=/usr/local/google_appengine/lib/django-1.5
export PYTHONPATH

to it. Type :w to save it and :q to quit. Log out of Terminal and log back in for the changes to take effect.

Creating a project

The first thing we need to do is create a python directory to hold our Django project.

cd
mkdir python
cd python

Once this has been created, issue the command to create our PollApp project.

python /usr/local/google_appengine/lib/django-1.5/django/bin/django-admin.py startproject PollApp

This creates the PollApp directory and some Django files. In order to make this an App Engine app, an app.yaml file needs to be created.

cd PollApp
vi app.yaml

Copy and paste the following into this file:

application: rem4me-me-pollapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: .*
  script: PollApp.wsgi.application

libraries:
- name: django
  version: "1.5"

Be sure and change rem4me-me-pollapp to be the same as your application identifier in the App Manager Console.

Django supports the WSGI standard which App Engine uses to host web applications. In the handlers: section, PollApp.wsgi.application represents the Django WSGI PollApp application. So all of the urls provided will run the Django PollApp project. In the libraries: section, we tell App Engine to include the Django 1.5 python libraries.

Next we need to launch GoogleAppEngineLauncher to specify the PollApp directory as an App Engine project. Once GoogleAppLauncher has launched, click the + button.

image

Enter the appropriate values and click Create. Next click the Deploy button to deploy the PollApp project to Google’s App Engine servers. Log in using your App Engine Console username and password. Once deployed, point your web browser to http://your-app-engine-application-id.appspot.com and watch Django in action! Pretty neat, huh?

Database setup

Now edit PollApp/settings.py and find the DATABASES settings. This is for relational databases like Oracle and MySQL. App Engine uses a NoSQL datastore, which we’ll be using. Since we don’t have any relational databases, delete the DATABASES settings so it looks like:

DATABASES = {}

And while we’re editing PollApp/settings.py, go ahead and set your TIME_ZONE. It defaults to Central (Chicago). I changed mine to Eastern (New_York).

Another thing to look at is the INSTALLED_APPS settings. The only thing we’ll be using is static files and the polls web application, which gets created, below.. So it can be changed to:

INSTALLED_APPS = (
    'django.contrib.staticfiles'
    'polls',
}

Another thing to look at is the MIDDLEWARE_CLASSES settings. App Engine SDK for Python actually comes with middleware for the NDB storage API (which we’ll be using, described below). I’m not sure exactly what it does, but the Google documentation recommends using it. So change your MIDDLEWARE_CLASSES settings to look something like:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'google.appengine.ext.ndb.django_middleware.NdbDjangoMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

The last thing to look at is the SECRET_KEY setting. This is a string that needs to be unique and not shared with others. I just made mine a random phrase of letters, numbers and symbols. I kept it long to keep people from easily guessing it. Also, this SECRET_KEY isn’t something you need to remember. You can set it and forget about it.

Go ahead and save the PollApp/settings.py file now.

Creating a Django web app

Now that we’ve created our Django project in ~/python/PollApp, it’s time to actually create the polling web application. In Terminal, type the following code to create the web application polls.

cd ~/python/PollApp/
python manage.py startapp polls

This will create a directory called polls which hold our models, views and templates for the polls web application.

Creating models

A model is a representation of the data stored in our App Engine datastore. In Python, a model is a class that represents an entity of some Kind in the datastore. The Python class name is the entity’s Kind.

The polls web application uses two models: Poll and Choice. Edit the polls/models.py file and change it to look like:

from google.appengine.ext import ndb

class Poll(ndb.Model):
    id = ndb.ComputedProperty(lambda self: self.key.id())
    question = ndb.StringProperty(indexed=False)
    pub_date = ndb.DateTimeProperty(auto_now_add=True)

class Choice(ndb.Model):
    id = ndb.ComputedProperty(lambda self: self.key.id())
    choice_text = ndb.StringProperty(indexed=False)
    votes = ndb.IntegerProperty(default=0)

As mentioned, above, NDB is the storage API we’ll be using for our datastore models in our App Engine web application. Both the Poll and Choice class inherit ndb.Model, which makes them model classes.

Both Poll and Choice have an id computed property. When a Poll or Choice is put into the datastore, App Engine assign’s it a Key. This Key has a unique id associated with it (much like a primary key in relational databases like Oracle and MySQL) but you can’t find that key value unless you use function id(). Django templates don’t allow function calls when the Poll and Choice objects are passed, so we have to setup a computed id property that gets that value for us.

The Poll entity has the question and pub_date properties. The question property is a string (up to 500 characters) which is indexed by default. We don’t index it because the PollApp/polls web application doesn’t use it when getting or querying entities from the datastore. The pub_date property is a date/time value which, by default, gets set to now (that’s what the auto_now_add=True does).

The Choice entity has the choice_text and votes properties. The choice_text property is a string which isn’t indexed because, again, we don’t use it to get or query Choice entities from the datastore. The votes property is an integer which defaults to value zero. It tells us how many times that Choice has been voted on.

Playing with the API

The rest of the Django tutorial describes to how populate the relational database models into their database and how to add Poll and Choice entities using the Django shell. Since we’re using a NoSQL datastore, we can’t really do any of that.

In Part 2, we’ll update our polls/models.py file with static methods and functions which we’ll use to populate the datastore and access the entities in it.

rgrjYUnLsGI

BackRead original postSend to a friend

Add comment

Add comment
Show all posts




-



 
 
 Who we are
Contact
More about us
Frequently Asked Questions
Register
Twitter
Blog
Explore
Apps
Artwork
Jobs
Knowledge
Events
People
Updates on identi.ca
Updates on Twitter
Content RSS   
Events RSS   

Participate
Groups
Forum
Add Content
Public API
About openDesktop.org
Legal Notice
Spreadshirt Shop
CafePress Shop
Advertising
Sponsor us
Report Abuse
 

Copyright 2007-2016 openDesktop.org Team  
All rights reserved. openDesktop.org is not liable for any content or goods on this site.
All contributors are responsible for the lawfulness of their uploads.
openDesktop is a trademark of the openDesktop.org Team