bad request(400) django-postgresql page error in gcp

475 Views Asked by At

my application was successfully deployed to google cloud platform. when i run command glcoud app browse it shows a blank page with bad request(400). this is the details of the error. my app runs well locally. where is the problem coming from? is there any command for checking if everything is perfectly setup in my google platform account?

Traceback (most recent call last):
  File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/env/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused

setting.py

from decouple import config
BASE_DIR = os.path.dirname(os.path.dirname
(os.path.dirname(os.path.abspath(__file__))))

try:

    SECRET_KEY = os.environ['SECRET_KEY']
    DEBUG = False
except KeyError:
    SECRET_KEY = config('SECRET_KEY')
    DEBUG = True
    ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
    'projects',
    'crispy_forms',

]
CRISPY_TEMPLATE_PACK= 'bootstrap4'
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 = 'mysite.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 = 'mysite.wsgi.application'


if os.getenv('GAE_APPLICATION', None):
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'HOST': '/cloudsql/connection name',
            'USER': ' user',
            'PASSWORD': 'project_password',
            'NAME': 'instance name',
           
        }
    }
else:
   
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'portfolio',
            'USER': 'postgres',
            'PASSWORD': 'local db password',
            'HOST': 'localhost',
            'PORT': 5432,

        }
    }

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',
    },]
STATIC_URL = 'https://storage.googleapis.com/newinstance_buc/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/')
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
# STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_URL = '/media/'

app.yaml

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT mysite.wsgi
env_variables:
  SECRET_KEY: 'password'
beta_settings:
    cloud_sql_instances: connection name

runtime_config:
  python_version: 3

automatic_scaling:
    min_num_instances: 1
    max_num_instances: 2
0

There are 0 best solutions below