1

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
Jan Hernandez
  • 4,414
  • 2
  • 12
  • 18
lvingstone
  • 171
  • 8
  • The is no command to check if everything is perfectly setup unfortunetelly, since there are a lot of independent components involved. As per the issue you are facing, you have to make sure to follow [these steps](https://cloud.google.com/sql/docs/postgres/connect-app-engine-standard) to connect your App Engine instance to Cloud SQL, also you might have to add the port on your `app.yaml` settings as well, like thise: `cloud_sql_instances: connection name=tcp:5432`. Let me know if this helps. – Ralemos Jul 31 '20 at 14:58
  • I added the port number , then I ran ```gcloud app deploy``` the deploy raised no issues. yet I got the same server error(500). I don't mind exhausting your options, if there is anything you want me to do. – lvingstone Jul 31 '20 at 17:20
  • Can you share your main.py or where you are actually creating the connection? – Ralemos Aug 04 '20 at 15:05
  • @ralemos **main.py** ```from mysite.wsgi import application app = application``` – lvingstone Aug 05 '20 at 11:04
  • your `main.py` only contains the above? Could you edit it's contents into the question so that it's better formatted? – Ralemos Aug 10 '20 at 14:40
  • @ralemos yes, that is all the content. an import and an instantiation(app). are there other things I need to specify in my main.py? – lvingstone Aug 10 '20 at 20:13
  • is this: `'HOST': '/cloudsql/connection name',` and this `'NAME': 'instance name',` correctly set? – Ralemos Aug 11 '20 at 16:58
  • yes. but where I am having doubt are my **main.py** , app.yaml and my settings.py is inside a setting directory. – lvingstone Aug 12 '20 at 06:02
  • yeah, I don't think the issue is in the `main.py` based on what you said that what you shared before was it's whole content. I am leaning towards something being wrong in the connection string itself. – Ralemos Aug 12 '20 at 13:54
  • ok, let me focus my debugging around that. I just hope it works – lvingstone Aug 12 '20 at 15:17
  • ok, let me know if you find anything. – Ralemos Aug 13 '20 at 14:49

0 Answers0