无法使用Heroku部署的应用程序登录Django管理界面

pep*_*319 6 python django heroku

有点奇怪的问题,但我无法使用Heroku部署的Django应用程序登录到管理界面。每当我输入正确的凭据时,都会收到一条错误消息,指出Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.我可以在Shell中以及在运行简单的本地django服务器时在本地进行身份验证,但不能在使用heroku部署的版本中进行身份验证。有什么想法吗?这是我的settings.py档案。它是使用Heroku配置的模板自动生成的,并使用此命令运行$ 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'
Run Code Online (Sandbox Code Playgroud)

Sih*_*nan 7

我遇到了类似的问题。然后我通过运行创建了一个超级用户:

$ heroku run python manage.py createsuperuser
Run Code Online (Sandbox Code Playgroud)

然后:

$ git push -f heroku master
$ heroku run python manage.py migrate
Run Code Online (Sandbox Code Playgroud)

它工作得很好。


小智 1

我面临类似的错误,但是我注意到你的settings.py中的一些东西我觉得应该改变,当你迁移到heroku时,默认数据库需要更改为postgresql,如下所示

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'mydatabase',
    'USER': 'mydatabaseuser',
    'PASSWORD': 'mypassword',
    'HOST': '127.0.0.1',
    'PORT': '5432',
}
Run Code Online (Sandbox Code Playgroud)

}

您将在 Heroku 应用程序的资源选项卡 -> 附加组件 -> postgresql -> 查看凭据中找到这些凭据。

请参阅以下链接了解详细信息:

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

此链接告诉您如何将 local_settings 与设置分开: https://djangogirls.gitbooks.io/django-girls-tutorial-extensions/heroku/

希望这可以帮助!