简体   繁体   中英

how to set DEBUG value automatically in django on Google App Engine

I think the question says it all..

I want to set DEBUG=False when running on Google App Engine but want to make it True on local machine..

(I am not using django-nonrel)

I do

app = webapp.WSGIApplication([
  ...
], debug = os.environ.get('SERVER_SOFTWARE', 'Dev').startswith('Dev'))

which is like Thomas Orozco's snippet, but defaults to enabling debug if the SERVER_SOFTWARE isn't set. That'll only happen if you happen to be using a bit of code outside of the app engine environment, so it's your call on how you want that to default.

Use the SERVER_SOFTWARE environment variable.

As instructed in the GAE docs :

SERVER_SOFTWARE : In the development web server, this value is "Development/XY" where "XY" is the version of the runtime. When running on App Engine, this value is "Google App Engine/XYZ".

So just do:

import os

def get_gae_debug():
    server =  os.environ.get("SERVER_SOFTWARE")

    if server is None:
        return False  # Unexpected, disable DEBUG.

    software, version = server.split("/", 1)
    return software == "Development"


DEBUG = get_gae_debug()

The most recent update for this syntax (place on your app settings.py):

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    DEBUG = False
else:
    DEBUG = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM