django DEBUG = False仍在调试模式下运行 [英] django DEBUG=False still runs in debug mode

查看:254
本文介绍了django DEBUG = False仍在调试模式下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在部署的服务器(在heroku上)上设置 DEBUG = False 时遇到问题。

I'm having issues setting my DEBUG = False on my deployed server (on heroku).

我没有相信我之前已经看过这种情况,但是将debug设置为false并不会摆脱项目的调试模式。这意味着当有500个错误时,它将显示错误,并且404错误将显示我的所有URL。

I don't believe I've seen this before, but setting debug to false is not getting rid of debug mode for my project. This means that when there are 500 errors it shows the error, and 404 errors are showing all my urls.

奇怪的是,当我登录到服务器并运行时,从中获取设置值 django.conf导入设置settings.DEBUG 显示为False,这是我为生产服务器设置的。 TEMPLATE_DEBUG 也设置为False。

What's strange is that when i log into the server and run get the setting value from django.conf import settings settings.DEBUG it shows as False, which is what I set it to for the production server. TEMPLATE_DEBUG is also set to False.

我不知道我以前在之前见过此内容DEBUG = False ,但它仍在调试模式下运行。有什么想法吗?

I don't know I've ever seen this before where DEBUG = False but it's still acting in debug mode. Any ideas?

我也想写这个小纸条,因为在人们遇到500或400错误时很常见将调试切换为False。我没有收到任何错误,我的项目就像在调试模式下一样运行。

Thought I'd put this little note too because it's very common for people to be getting 500 or 400 errors when switching debug to False. I am not getting any errors, my project is just acting like it's in DEBUG mode.

# settings/dev.py
from .base import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

if not DEBUG:
    ALLOWED_HOSTS = ['localhost']

SECRET_KEY = 'localsecret1234123412341234'


# settings/prod.py
from .base import *

import dj_database_url, os

DEBUG = os.environ.get("DEBUG", False)
TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = ['mydomain.com']
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

DATABASES = {}
DATABASES['default'] =  dj_database_url.config()


推荐答案

我将其作为对后来的人们的实际答案。

I'll put this as an actual answer for people that come after.

可能会发生三个主要方面,其中 this基本上是:

There are three main areas that this can happen, where 'this' is basically:


我已经更改了设置,但似乎未反映在我的应用程序的操作中!

I've changed something in my settings, but it doesn't seem to be reflected in the operation of my app!




  1. 更改代码后未重置服务器。

  2. 本地和开发设置没有被覆盖/或以意外方式被覆盖

  3. 本地环境变量覆盖了对其进行硬编码的尝试

在Heroku上,如本例所示,环境变量这样设置的更通用的指南。可在此处获得Linux环境。

On Heroku, as in this example, Environment Variables are set this way. A more general guide to using them in Linux enviroments is available here.

使用问题中的示例,您可以看到正在使用的环境变量。 :

Using an example from the question, you can see the environment variables being used in:

SECRET_KEY = os.environ["DJANGO_SECRET_KEY"] 

这使用os库进入系统,并检查名为 DJANGO_SECRET_KEY 的环境变量。

This uses the os library to go out to the system, and check for an environment variable called DJANGO_SECRET_KEY.

一个更好的例子是:

DEBUG = os.environ.get("DEBUG", False)

这很好,因为它试图从环境中获取数据,如果失败,则使用元组中的第二个值作为默认值:在这种情况下为False。

This is good because it tries to get it from the environment, and if that fails, uses the second value in the tuple as the default: in this case, False.

在调试过程中,您可以使用linux shell中的 printenv 来查找设置。淘汰所有可用的EnvVar。如果不存在,则可以以下格式设置它们:

During the course of debugging, you can hunt down your settings using printenv from the linux shell, which will print out all of the available EnvVars. If they're not there, you can set them in the format:

export DJANGO_SETTINGS_MODULE = mysite.settings

在这种情况下,最好取消设置环境变量,而不是强制类型转换,根据此相关答案。

In this instance, it's probably better to unset the environment variable, rather than typecasting, as per this related answer.

这篇关于django DEBUG = False仍在调试模式下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆