<Part One>
Step zero: Download the software and Set up the environment
Following steps are required only Once to set up the Google App Engine/Python/Django development environment in Windows
- Download and install the latest Python (I'm using Python 2.5) from the Python website
- Download and install the latest Python pywin32 add-on http://sourceforge.net/projects/pywin32/
- Download the App Engine SDK. Follow the instructions on the download page to install the SDK on your computer.
- Download the Google App Engine Helper for Django at http://code.google.com/p/google-app-engine-django.
- Extract the helper source archive into a directory, c:downloadappengine_helper_for_django
Step one: Start the default server in localhost
- Sign up for an App Engine account and create an Application. i.e smartwebnote http://smartwebnote.appspot.com/
- Copy the c:downloadappengine_helper_for_django into the your working directory, c:demo and rename the folder appengine_helper_for_django to whatever the name you want i.e. coolsite.
- cd c:democoolsite
- c:democoolsite> manage.py runserver and go check the application running http://127.0.0.1:8080/
- In the command line, you may see
"WARNING:root:Could not read datastore data from c:...localtempdjango_google-app-engine-django.datastore"
Ignore the WARING for now
Step two: Create an application and Handle the http request with "Hello World" response
- C:democoolsite>manage.py startapp smartwebnote
- Edit urls.py to add
urlpatterns = patterns('smartwebnote.views',
(r'^$', 'index'),
) - cd smartwebnote. Edit views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello world') - refresh the server at http://127.0.0.1:8080/ you should get the "Hello World"!
- Edit models.py
from google.appengine.ext import db
from appengie_django.models import BaseModel
class Note(BaseModel):
content = db.TextProperty()
last_updated = db.DateTimeProperty(auto_now=True) - Create a few Note objects using the Python shell
> manage.py shell
>>> from smartwebnote.models import Note
>>> note = Note(content='my first note')
>>> note.save()
datastore_types.Key.from_path('Note', 1, _app=u'google-app-engine-django')
>>> note = Note(content='my second note')
>>> note.save()
datastore_types.Key.from_path('Note', 2, _app=u'google-app-engine-django')
>>> results = Note.all().fetch(limit=10)
>>> print results
[<smartwebnote.models.Note object at 0x01DFEA70>, <smartwebnote.models.Note object at 0x01DFEB10>] - We have successfully created Note objects using shell. Now let's use the Development Console manage the objects
- goto: http://localhost:8080/_ah/admin/datastore. Click the interactive Console. Add the following code
from smartwebnote.models import Note
note = Note(content='my first note')
note.save()
print note - go back to the Datastore Viewer and search for 'Note'. You should see the object you just created! Using the console to create a few more objects and try the delete function as well.
Step four: Add Views and Templates to display the list
Views in Django = Controller in MVC pattern
Templates in Django = View in MVC pattern
The generic views provided by Django can not be supported in Google App Engine
- Test the query in the Application Interactive Console
object_list = Note.all()
for o in object_list:
print o.content
You should get a list of objects you created in the Console - Edit the urls.py and add the pattern for list
urlpatterns = patterns('smartwebnote.views',
(r'^$', 'index'),
(r'^list/$', 'list'),
) - Edit the smartwebnote/views.py
query = Note.all()
def list(request):
return render_to_response('smartwebnote/note_list.html',
{'object_list':query}) - Create templates/smartwebnote under c:/demo/coolsite/
- Create note_list.html
<html>
<body>
<p> <a href ="create"> Create a new note</a></p>
<table id="myTable">
<thead>
<tr>
<th>Delete</th>
<th>Note</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody>
{% for note in object_list %}
<tr valign="top">
<td>
<a href="delete/{{ note.id }}">Delete</a>
</td>
<td>
<a href="{{ note.id }}">{{ note.content }}</a>
</td>
<td>
{{ note.last_updated|date:"M j, Y, P" }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html> - Reload the page: http://127.0.0.1:8080/list/
This completes the Part one of this tutorial. Hopefully this helps. Feel free to contact me if you have questions and comments.