2017年5月23日 星期二 雨

 这次GAE上从Pyhon2.5升级到pyhon2.7,果然没有预期那样顺利,其中最主要是我在当时的项目里用了Django。我只有一个没有用到session的中国象棋是ok的,其余的project调试了很久。

 用Django是有原因的,我之所以知道Python是因为Bob,然后Bob想和我一起创业做个游戏类的网站,Bob还花了两千块买了Logo之类的,写代码时Bob介绍了Django,项目后来没出两月就流产了。那时正是GAE和SNS兴起的时候,我参加了一个GAE开发者大会,听到别人介绍了pyxn的项目,我就继续用了Django。那时Django的版本记得好像还是0.96版,我用了使用Google App Engine Helper for Django,其实与其用了Django,还不如说我只是用了它其中一小部分。

 GAE上强制升级到Python2.7,我也不得不跟着升级了,我个人老项目的主要修改:
  1. md5替换成hashlib

  2. 删除了Google App Engine Helper的appengine_django目录,用系统自带的Django。

  3. manage.py 修改前: [code]from appengine_django import InstallAppengineHelperForDjango InstallAppengineHelperForDjango() from django.core.management import execute_manager try: import settings # Assumed to be in the same directory. except ImportError: import sys sys.stderr.write(“Error: Can’t find the file ‘settings.py’ in the directory containing %r. It appears you’ve customized things.\nYou’ll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it’s causing an ImportError somehow.)\n” % file) sys.exit(1) execute_manager(settings)[/code] 修改后: [code]#!/usr/bin/env python import os import sys

    os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “settings”) from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)[/code]

  4. main.py下: 之前是:[code] from appengine_django import InstallAppengineHelperForDjango from appengine_django import have_django_zip from appengine_django import django_zip_path InstallAppengineHelperForDjango() def main():

Ensure the Django zipfile is in the path if required.

if have_django_zip and django_zip_path not in sys.path: sys.path.insert(1, django_zip_path)

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() [/code]

现在是: [code]application = django.core.handlers.wsgi.WSGIHandler()[/code]

  1. app.yaml 修改前: [code] runtime: python …
  • url: /.* script: main.py [/code]

修改后: [code] runtime: python27 … threadsafe: false … libraries:

  • name: django version: “1.4”
  • name: MySQLdb version: 1.2.4

env_variables: DJANGO_SETTINGS_MODULE: ‘settings’ …

  • url: /.* script: main.application [/code]
  1. models.py 修改前 [code]from appengine_django.models import BaseModel[/code]

修改后 [code]from google.appengine.ext.db import Model as BaseModel[/code]

  1. settings.py 下面这段是新增的,Django新版本的样式。下面这段我搞了一天多。 修改前: [code] DATABASE_ENGINE = ‘appengine’ # ‘postgresql_psycopg2’, ‘postgresql’, ‘mysql’, ‘sqlite3’ or ‘oracle’. 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.

MEDIA_URL = ‘smedia’

TEMPLATE_LOADERS = ( ‘django.template.loaders.filesystem.Loader’, ‘django.template.loaders.app_directories.Loader’,

‘django.template.loaders.eggs.Loader’,

)

INSTALLED_APPS = ( ‘appengine_django’, [/code]

修改后 [code]DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.mysql’, ‘HOST’: ‘/cloudsql/test’, ‘NAME’: ‘polls’, ‘USER’: ‘’, ‘PASSWORD’: ‘’, } } MEDIA_URL = ‘/smedia/’ TEMPLATE_LOADERS = ( ‘django.template.loaders.filesystem.load_template_source’, ‘django.template.loaders.app_directories.load_template_source’,

‘django.template.loaders.eggs.load_template_source’,

)

INSTALLED_APPS = (

‘appengine_django’,

[/code]

  1. views.py #from appengine_django.sessions.models import Session #yobin 20170521 remove for py2.7

request.session不能用了,我在GAE光找看log的地方就找了好一会儿。上面的settings.py里写的mysql是我随便写写的,我并没有开通Cloudsql,所以实际运行时会连不上而报500错误。另外,也尝试了各种database ENGINE,比如sqlite3之类的都不行。

我搜了很多文章,也找了很多djangae这样的项目,都不能用,最后还是自己一步步硬调出来了。想起当年写Django的Template不好用,再加上这回的Porting,感觉Django真心不好用,我当年写完校内应用就没有再用过Django了。