Search code examples
djangogoogle-app-enginesessiondjango-admindjango-settings

Django Admin Error (Always require login couple of time) when DEBUG = False (Productions)


I've just deployed Django 2.2 in Google App Engine.

Everything works fine except this one. The admin page requires login couple of time till I can finally go to the dashboard. This happens also when I click on the Post model to create a new post, I need to login again. I'm not sure why but this happen when the DEBUG = False.

I take a look at this question and his answer but it might bit different for the debug.

Does anyone have the same issue as mine?

Here's my settings.py file:

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


BASE_URL = "https://example.com"

ALLOWED_HOSTS = ['*']

# ALLOWED_HOSTS = [
#     'www.notnoob.com',
#     'notnoob.com'
# ]


# SESSION_COOKIE_NAME
# SESSION_COOKIE_DOMAIN = None
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.sitemaps',
    'home',
    'blog',                                                                 
    'contact',
    'ckeditor',
    'ckeditor_uploader',
    'taggit',
    'meta',
    'django_filters',

]

...
...

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 = 'dj.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'dj/templates')],
        '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 = 'dj.wsgi.application'

.....
.....

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

DATABASES['default']['HOST'] = 'cloudsql/djangoblogdb'
if os.getenv('GAE_INSTANCE'):
    pass
else:
    DATABASES['default']['HOST'] = 'localhost'


# Password validation
# https://docs.djangoproject.com/en/2.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',
    },
]

Not sure why but when the DEBUG = True, everything goes well.

My admin.py:

from django.contrib import admin
from blog.models import Post, MetaModel

admin.site.site_header = "Welcome To iColdPlayer"
admin.site.site_title = "iColdPlayer Administrator"
admin.site.site_index = "iColdPlayer"

# site = admin.AdminSite(name="iColdPlayer")

# class PostAdmin(admin.ModelAdmin):
#     pass
#
# class MetaModelAdmin(admin.ModelAdmin):
#     pass

admin.site.register(Post)
admin.site.register(MetaModel)

Edit: I'm using Flexible Environment


Solution

  • The problem is that you are storing your data in SQLITE3, and probably you have more than one GAE running.

    So you login on Instance 1, but is redirected to Instance 2 (stateless.....)

    And guess what, on 2, you are not logged in, so you have to do it again.

    To fix, change your app.yml to set maximum instances to 1 or use a mysql outside GAE to store your sessions....

    Let me know if that solves it