本文介绍了我的django应用程序中的环境设置被设置为调试错误,但在生产中它的行为就好像它的真实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的django应用程序的本地和生产设置,从底层导入

I have local and production settings for my django app that import from base like so

from .base import *

try:
    from .local import *
except:
    pass

try:
    from .production import *
except:
    pass

在我的基础我有

import os
import dj_database_url
from .my_pass import SECRET, EMAIL_PASSWORD, EMAIL_USER

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))



SECRET_KEY = SECRET

DEBUG = False

在我的本地

import os
import dj_database_url
from .my_pass import SECRET

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))


SECRET_KEY = SECRET

DEBUG = True

在我的制作中

from django.conf import settings

if not settings.DEBUG:
    import os
    import dj_database_url


    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
    PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))




    SECRET_KEY = os.environ['SECRET_KEY']

    DEBUG = False

但是当我通过这样做在生产中进行测试时

But when I tested it out in production by doing this

example.com/jnxejnn

example.com/jnxejnn

它显示了一个URL列表,好像DEBUG设置为true。为什么是这样?

it showed me a list of urls as if DEBUG was set to true. Why is that?

推荐答案

查看设置文件的顺序:


  • 首先从base导入:DEBUG is False

  • 然后从本地导入:DEBUG is True

  • 那么它从生产中导入:在这一点上,DEBUG是True,所以你的如果没有设置.DEBUG:块从未输入,并且DEBUG没有再设置为False。

  • first it imports from base: DEBUG is False
  • then it imports from local: DEBUG is True
  • then it imports from production: at this point, DEBUG is True, so your if not settings.DEBUG: block is never entered, and DEBUG is not set to False again.

因此,DEBUG保持为True,因为它在本地设置文件中设置。

Thus, DEBUG remains True, as it is set in the local settings file.

我不知道你的如果不是settings.DEBUG 检查的目的是什么,但我认为如果你消除这种情况,它将按照你的期望工作。

I'm not sure what the purpose of your if not settings.DEBUG check is, but I think if you eliminate that condition, it will work as you expect.


虽然上述确实回答了你的为什么是这样的问题,但它并没有真正有助于满足您的需求, d建议对您的设置文件进行修改,如下所示:

Though the above did answer your question of "Why is that?", it doesn't really help meet your needs, so I'd recommend making a modification to your settings file like so:

from .base import *

if os.environ['DJANGO_SERVER_TYPE'] == 'local':
    try:
        from .local import *
    except:
        pass

if os.environ['DJANGO_SERVER_TYPE'] == 'production':
    try:
        from .production import *
    except:
        pass

这篇关于我的django应用程序中的环境设置被设置为调试错误,但在生产中它的行为就好像它的真实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:05