Django + GoogleAppEngine错误:DJANGO_SETTINGS_MODULE未定义

3 投票
1 回答
1029 浏览
提问于 2025-04-16 12:33

我正在学习如何使用Django,并且我在使用Google App Engine。我把最新的Django文件夹放在和我的主文件同一个目录下。

在本地运行的时候一切都很正常。但是当我把它部署到网页上运行时,却出现了服务器错误。

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

main.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()

settings.py:

import os

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

1 个回答

2

如果你在使用环境变量时遇到问题,可以尝试直接配置设置(具体可以参考这个链接: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)

撰写回答