Home > database >  Django: set DEBUG = False causes Server Error (500)
Django: set DEBUG = False causes Server Error (500)

Time:03-03

the templats work when DEBUG = True but change to False gives Server Error (500) i used django 3.2.8

This is views.py file

def test(request):
context = {}
if request.method == "POST":
    uploaded_file = request.FILES['document']
    print(uploaded_file)
    if uploaded_file.name.endswith('.csv'):
        #save file in media folder
        savefile = FileSystemStorage()
        name = savefile.save(uploaded_file.name, uploaded_file) #name of the file
        #know where to save file
        d = os.getcwd() #current directory of the project
        file_directory = d   '\media\\'   name
        readfile(file_directory)
        return redirect(results)
    else:
         messages.warning(request, 'File was not uploaded. Please use csv or xlsx file extension!')

return render(request, 'test.html', {})

#project.csv def readfile(filename): global rows, columns, data, my_file, missing_values, mydict, dhead, dtail, dinfo, ddesc, dcor, graph, dheat

my_file = pd.read_csv(filename, engine='python', index_col = False, sep='[: ; , | -]', error_bad_lines=False)
data = pd.DataFrame(data=my_file)
#my_file= read_file(filename)

data = pd.DataFrame(data=my_file)

mydict = {

    
    "data ": data.to_html(),
    
}

#rows and columns
rows = len(data.axes[0])
columns = len(data.axes[1])

#find missing data
missingsings = ['?','0','--']
null_data = data[data.isnull().any(axis=1)]
missing_values = len(null_data)

def results(request):

    message = 'I found '    str(rows)   ' rows and '   str(columns)   ' columns. Missing data are: '   str(missing_values)
    #message = info
    messages.warning(request, message)

    return render(request, 'results.html', context = mydict)

This is settings.py file

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('secret_key')

# SECURITY WARNING: don't run with debug turned on in production!

DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'widget_tweaks',
    'dtest',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'datestproject.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'datestproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbtest',
        'USER': 'postgres',
        'PASSWORD':'850636',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Riyadh'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'





# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Email Settings

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = env('email_user')
EMAIL_HOST_PASSWORD = env('email_pass')
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

CodePudding user response:

Django does not support file serving in production (have a look here). If you want to save and serve files in production with debug=False try to switch to another storage backend.

CodePudding user response:

You can debug the error by this setting:

DEBUG_PROPAGATE_EXCEPTIONS = True

See the reference here:- https://docs.djangoproject.com/en/4.0/ref/settings/#debug-propagate-exceptions

  • Related