设置Django应用程序以在谷歌应用程序引擎GAE(app.yaml& main.py)上工作

时间:2017-08-28 14:08:45

标签: django google-app-engine pycharm

我有一个工作的Django应用程序,我能够在Heroku上运行。该结构的项目命名为“无标题”。以及一个名为' web'的应用程序这样的结构是:


project_root

模板

--->的初始化的.py
---> settings.py
---> urls.py
---> wsgi.py


--->的初始化的.py
---> admin.py
---> apps.py
---> models.py
---> tests.py
---> urls.py
---> views.py

这是一个非常基本的应用程序,我可以在GAE之外(本地和Heroku)工作,但是,我仍然遇到GAE的app.yaml和main.py要求。

我的app.yaml是:

application: seismic-interpretation-institute-py27
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app

libraries:
- name: django
  version: "latest"

和我的main.py(从PyCharm生成)是

import os,sys

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

# 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

os.environ['DJANGO_SETTINGS_MODULE'] = 'untitled.settings'

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

最后,本地运行时报告的输出是

enter image description here

似乎是错误,

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

导致我的问题。我不确定如何解决它。

1 个答案:

答案 0 :(得分:0)

尝试替换

from django.conf import settings
settings._target = None

os.environ['DJANGO_SETTINGS_MODULE'] = 'untitled.settings'

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "untitled.settings")
from django.conf import settings
settings._target = None