python - Django + GoogleAppEngine 错误 : DJANGO_SETTINGS_MODULE is undefined

标签 python django google-app-engine

因此,我正在学习如何使用 Django,并且正在使用 Google App Engine。我在与主目录相同的目录中有最新的 Django 目录。

当我从本地运行它时,它工作正常。但是,当我部署它并从网页运行它时,出现服务器错误。

这是我的 main.py 和 settings.py 的代码

主要.py:

import os
import settings
from django.template import Context, Template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

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

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, World!\n')
        t = Template('It is now {% now "jS F Y H:i" %}')
        c = Context({ })
        self.response.out.write(t.render(c))

application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

设置.py:

import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG
TIME_ZONE = 'America/Whitehorse'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1

最佳答案

如果您在使用环境变量时遇到问题,请尝试直接配置设置(请参阅 http://docs.djangoproject.com/en/dev/topics/settings/#using-settings-without-setting-django-settings-module)

import settings
import django.conf

# Filter out all the non-setting attributes of the settings module
settingsKeys = filter(lambda name: str(name) == str(name).upper(), 
                      dir(settings))

# copy all the setting values from the settings module into a dictionary
settingsDict = dict(map(lambda name: (name, getattr(settings, name)), 
                        settingsKeys))

django.conf.settings.configure(**settingsDict)

关于python - Django + GoogleAppEngine 错误 : DJANGO_SETTINGS_MODULE is undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5122414/

相关文章:

python - 使用应用程序的 sql 文件夹内的 sql 文件将初始数据提供到 sql 表中在 django1.9 中不起作用

python - Google App Engine Python 中是否有等效的 SQL View ?

python - Pandas 旋转一列,同时使用相同的列值作为列标题

c# - python 到 c# (关于服务器请求)

javascript - 如何将表单数据从外部html文件发送到django?

python - 使用 GenericForeignKey 进行 Django 模型设计

python - Google Sites API + OAuth2(在 Appengine 上)

python - 谷歌应用引擎 - Python : How to use Namespaces and Ancestors?

python - python中的 “unpacking” nltk错误

python - 有没有办法在 Python 中多次格式化一个字符串?