Django在Google App Engine上遇到的困难

时间:2010-05-18 20:43:29

标签: python django google-app-engine

我有一个工作正常的Django 1.1.1项目。我正在尝试将其导入Google App Engine。

我正在尝试关注这些instructions.

我在开发服务器上运行它,导致导入错误:

ImportError at /
No module named mysite.urls

这是 mysite /

的文件夹结构
app.yaml
<DIR>          myapp
index.yaml
main.py
manage.py
<DIR>          media
settings.py
urls.py
__init__.py

的app.yaml:

application: mysite
version: 1
runtime: python
api_version: 1

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

来自 settings.py

ROOT_URLCONF = 'mysite.urls'

我做错了什么?

更新

现在我收到了这个错误:

Request Method:     GET
Request URL:    http://localhost:8082/
Exception Type:     AttributeError
Exception Value:    'module' object has no attribute 'autodiscover'
Exception Location:     C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py in LoadModuleRestricted, line 1782

main.py

import logging, os

# 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

# Must set this env var before importing any part of Django
# 'project' is the name of the project created with django-admin.py
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

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

def log_exception(*args, **kwds):
    logging.exception('Exception in request:')

# Log errors.
django.dispatch.dispatcher.connect(
    log_exception, django.core.signals.got_request_exception)

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

engineapp / 的目录结构:

<DIR> mysite
app.yaml
index.yaml
main.py

engineapp / mysite 的目录结构:

<DIR> myapp
<DIR> media
__init__.py
initial_data.json
manage.py
settings.py
urls.py

我觉得我越来越近了,但仍然没有。

3 个答案:

答案 0 :(得分:2)

尝试将ROOT_URLCONF更改为'urls'。我不认为在App Engine上运行时,应用程序的父目录(在App Engine意义上,而不是Django意义上)目录在sys.path上,这意味着它没有看到mysite作为Python包/模块。

编辑以跟上编辑过的问题:

现在听起来你无意中使用了Django 0.96但是期望使用Django 1.1+。在App Engine上import django时,除非明确告诉App Engine要使用其他版本,否则最终会得到0.96。

这样的东西
from google.appengine.dist import use_library
use_library('django', '1.1')
来自jonmiddleton's answer

应该可以解决问题。请注意,为了在开发服务器上使用它,您必须安装自己的Django 1.1副本,因为它没有与SDK捆绑在一起。

请注意,据我所知,您在App Engine上使用Django管理站点时不会有任何运气。

答案 1 :(得分:1)

这是我的main.py:

import os
import sys
import logging

# Google App Hosting imports.
from google.appengine.ext.webapp import util
from google.appengine.dist import use_library

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
sys.path.append("/home/brox/tmp/mysite")

use_library('django', '1.1')

# Enable info logging by the app (this is separate from appserver's
# logging).
logging.getLogger().setLevel(logging.DEBUG)

def log_exception(*args, **kwds):
  logging.exception('Exception in request:')

# Force sys.path to have our own directory first, so we can import from it.
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

# 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

# Log errors.
django.dispatch.Signal.connect(
   django.core.signals.got_request_exception, log_exception)

# Unregister the rollback event handler.
django.dispatch.Signal.disconnect(
django.core.signals.got_request_exception,
django.db._rollback_on_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()

正如您所看到的,还有其他路径,django错误记录有点不同......

希望它对你有所帮助。

答案 2 :(得分:0)

我没有将纯django与app引擎一起使用。但是从我读过的内容来看,你需要使用django app engine-patch才能使它工作。您可以阅读并从http://www.allbuttonspressed.com/projects下载。