How can I tell whether my Django application is running on development server or not?

后端 未结 13 1575
别跟我提以往
别跟我提以往 2020-12-05 03:37

How can I be certain that my application is running on development server or not? I suppose I could check value of settings.DEBUG and assume if DEBUG

13条回答
  •  庸人自扰
    2020-12-05 04:27

    Relying on settings.DEBUG is the most elegant way AFAICS as it is also used in Django code base on occasion.

    I suppose what you really want is a way to set that flag automatically without needing you update it manually everytime you upload the project to production servers.

    For that I check the path of settings.py (in settings.py) to determine what server the project is running on:

    if __file__ == "path to settings.py in my development machine":
        DEBUG = True
    elif __file__ in [paths of production servers]:
        DEBUG = False
    else:
        raise WhereTheHellIsThisServedException()
    

    Mind you, you might also prefer doing this check with environment variables as @Soviut suggests. But as someone developing on Windows and serving on Linux checking the file paths was plain easier than going with environment variables.

提交回复
热议问题