pepper5319
pepper5319

Reputation: 667

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'

Upvotes: 12

Views: 10702

Answers (4)

Raj Tripathi
Raj Tripathi

Reputation: 61

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

Upvotes: 3

Kabir Lovero
Kabir Lovero

Reputation: 56

Maybe this will help someone else. After some thorough search on the net, I had still the issue and could not login into the admin page in production. Therefore , I had to come up with a workaround. I created a new user in my local environment (checked if I could log in with it on localhost, and it worked), copied the sqlite file located in the app folder,which is called db.sqlite3 , and copied it onto the production server in the same location. Restarted the app, and i was able to login. I came up with this after i saw on the command line that the user i just created did not exist.

heroku run python /app/src/manage.py changepassword  -a appname

which returned:

CommandError: user 'yourusername' does not exist

Upvotes: 0

Sihat Afnan
Sihat Afnan

Reputation: 882

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.

Upvotes: 12

sqlearner
sqlearner

Reputation: 65

I am facing similar error, but however I have noticed few things in your settings.py I feel should be changed, when you are migrating to heroku the default database needs to be changed to postgresql like this

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'mydatabase',
    'USER': 'mydatabaseuser',
    'PASSWORD': 'mypassword',
    'HOST': '127.0.0.1',
    'PORT': '5432',
}

}

You will find these credentials in your Heroku App's Resources tab->add-ons->postgresql->view credentials.

Refer to these links for detailed information:

https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-DATABASES

This link tells you how to separate local_settings from settings: https://djangogirls.gitbooks.io/django-girls-tutorial-extensions/heroku/

Hope this helps!

Upvotes: 1

Related Questions