Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Log in to Django Admin Interface with Heroku Deployed App

Kind of a strange question, but I cannot login to my admin interface with a Heroku deployed Django app. Whenever I enter in the correct credentials, I get an error stating Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive. I can authenticate locally in the shell, and when I am running a simple local django server, but not while on the heroku deployed version. Any ideas why? Here is my settings.py file. It was automatically generated with the Heroku configured template, ran with this command $ django-admin startproject --template=https://github.com/heroku/heroku-django-template/archive/master.zip --name=Procfile myproject

settings.py

import os
import dj_database_url

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

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'pepfolio.urls'

TEMPLATES = (
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
            'debug': DEBUG,
        },
    },
)

WSGI_APPLICATION = 'pepfolio.wsgi.application'


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

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

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/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

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

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
like image 689
pepper5319 Avatar asked Apr 29 '16 12:04

pepper5319


People also ask

Can I use Django with Heroku?

When you deploy to Heroku, the dependencies you specify in your requirements. txt file are automatically installed before app startup. If you're using Django, the collectstatic command also runs automatically during the deployment process.


2 Answers

I faced the similar problem. Then I created a superuser by running:

$ heroku run python manage.py createsuperuser

Then:

$ git push -f heroku master
$ heroku run python manage.py migrate

It worked all fine.

like image 107
Sihat Afnan Avatar answered Sep 20 '22 06:09

Sihat Afnan


This worked for me:

heroku run python manage.py createsuperuser -a <app_name>

After creating the superuser, refresh your app & make sure to use a first capital letter in the username section like "Raj" instead of "raj".

Edit:- Added the -a flag in front of <app_name>, without which heroku will throw an error

like image 39
Raj Tripathi Avatar answered Sep 24 '22 06:09

Raj Tripathi