📜 ⬆️ ⬇️

Django app on google app engine

About Google App Engine have not heard already probably just lazy. Using the Django framework in conjunction with GAE provides developers with convenient tools to quickly create web applications.

This tutorial describes how to create a simple Django application using this platform. It is assumed that you already have a Google App Engine account.


1. First you need to register the application name in GAE and install the toolkit . The Google App Engine runs only with Python 2.5 (2.5.1, 2.5.2).
')
2. Create a folder for your application. Name and location does not matter. My folder is called " dvk ".

3. In the application folder, create the file main.py
import os, sys

os.environ ["DJANGO_SETTINGS_MODULE"] = "dvk.settings"

ROOT_PATH = os.path.dirname (__ file__)
sys.path.append (ROOT_PATH)

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

# 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 ()

This file is launched when the user opens a page of your web application in the browser.

4. Create app.yaml file
application: dvk
version: 1
runtime: python
api_version: 1

handlers:
- url: / static
static_dir: static

- url: /.*
script: main.py

The name of the application naturally substitute to your liking :)

5. From the application folder, run the command to create a new Django project:
django-admin.py startproject dvk

A small note: In total, we will have 2 project folders - a GAE-project folder and a Django-project folder inside it. The external project serves as a wrapper.
dvk /
main.py
app.yaml
dvk /
manage.py
settings.py
urls.py

In the settings file of the Django-project settings.py, you need to delete everything related to the database settings, admin panel, authentication and sessions. Google App Engine does not support Django models, so these modules will not work.

6. Now you can test our GAE + Django application.
cd ...
dev_appserver.py dvk

INFO 2008-04-08 19: 08: 10,023 appcfg.py] Checking for updates to the SDK.
INFO 2008-04-08 19: 08: 10,384 appcfg.py] The SDK is up to date.
INFO 2008-04-08 19: 08: 10,404 dev_appserver_main.py] Running application dvk on port 8080: localhost : 8080

Open a browser with the address 127.0.0.1 : 8080 / and you will see the standard Django page with the message It worked!

7. Create a Django application in the Django project folder.
manage.py startapp main


8. Now is the time to add a data model. We will create a simple Visitor class that will store information about all visitors to the site.
# ... / dvk / dvk / main / models.py
from google.appengine.ext import db

class Visitor (db.Model):
ip = db.StringProperty ()
added_on = db.DateTimeProperty (auto_now_add = True)

When you add or change models in Google App Engine, you do not need to synchronize with the database. It runs automatically.

9. Now create a procedure controller. It will record the parameters of our web page visitor in the database and display a list of all visitors.
# ... / dvk / dvk / main / views.py
from django.http import HttpResponse
from dvk.main.models import Visitor

def main (request):
visitor = Visitor ()
visitor.ip = request.META ["REMOTE_ADDR"]
visitor.put ()
result = ""

visitors = Visitor.all ()
visitors.order ("- added_on")
for visitor in visitors:
result + = visitor.ip + u "visited on" + unicode (visitor.added_on) + u ""

return HttpResponse (result)


10. In the URL dispatcher file, we indicate that our procedure is responsible for processing the request for the main page of the site.
# ... / dvk / dvk / main / urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns ("",
(r "^ $", "mashname.main.views.main"),
)

11. Test your application by opening the browser window at 127.0.0.1 : 8080 /. Each time a web page is updated, a new line will be added and, accordingly, a new Visitor object will be written to the database. GAE also automatically creates the application admin panel - 127.0.0.1 : 8080 / _ah / admin /

12. Now you can upload our application to GAE: appcfg.py update dvk
At the first execution, the command will ask for authorization.

Used materials:
Running Django on Google App Engine
Django on Google App Engine in 13 simple steps

Cross post from my blog

Source: https://habr.com/ru/post/25696/


All Articles