Django管理员升级到2.1后无法登录

goz*_*lli 6 django django-urls django-admin django-authentication django-settings

将旧的Django 1.8升级到2.1后,当我尝试登录我的管理站点时,收到404消息:

Using the URLconf defined in <mysite>.urls, Django tried these URL patterns, in this order:
[...]
The current path, login/, didn't match any of these.
Run Code Online (Sandbox Code Playgroud)

我猜这是真的,因为它应该__admin/login像我一样urls.py:

urlpatterns = [
    ...
    path(r'__admin/', admin.site.urls),
    ...
]
Run Code Online (Sandbox Code Playgroud)

但:

  • GET请求/__admin/login按预期返回登录页面
  • 问题仅发生在生产中(在VPS上有乘客WSGI),而不是在localhost上
  • 通过手动登录用户manage.py shelldjango.contrib.auth.authenticate() 正常工作
  • 不幸的是,升级发生在VPS机器中的迁移的同时,因此数据库也可能存在问题.

Python版本(在VPS上):3.6.7
Django版本(在VPS上):2.1.3(也尝试过2.1.4)

编辑1

我已经注释掉了所有其他内容urls,因此现在唯一可用的URL是__admin.现在,当我登录时,我收到此错误:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

     1. __admin/

The current path, login/, didn't match any of these.
Run Code Online (Sandbox Code Playgroud)

看起来django正在寻找login/而不是__admin/login.这可能吗?可能导致这种情况?

编辑2 添加了设置文件.

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.contrib import messages

PROJ_SHORTNAME = '<removed>'
PROJ_NAME      = '<removed>'

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = <removed>

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

ALLOWED_HOSTS = []

# 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',
    'bootstrap3',
    'mysite',
    'blog',
    'envelope',         # required for contact page
    'crispy_forms',     # required for contact page
    'honeypot',         # required for contact page
    'braces',           # required for contact page
    'disqus',
    'markdownx',
)

MIDDLEWARE = (
    '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 = '<removed>.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.template.context_processors.media',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'mysite.context_preprocessors.proj_name',
            ],
        },
    },
]

WSGI_APPLICATION = '<removed>.wsgi.application'

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': PROJ_SHORTNAME,
        'HOST': 'localhost',
        'USER': '<removed>',
        'PASSWORD': '<removed>',
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-gb'
TIME_ZONE = 'Europe/London'
USE_I18N = True
USE_L10N = True
USE_TZ = True

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

STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL  = '/media/'

## Markdown 
MARKDOWN_EXTENSIONS = ['extra', 'codehilite']

MESSAGE_TAGS = {
    messages.DEBUG: 'debug',
    messages.INFO: 'info',
    messages.SUCCESS: 'success',
    messages.WARNING: 'warning',
    messages.ERROR: 'danger' # 'error' by default
}

## contact (envelope)
HONEYPOT_FIELD_NAME = 'email2'
CRISPY_TEMPLATE_PACK = 'bootstrap3'
ENVELOPE_USE_HTML_EMAIL = False
ENVELOPE_SUBJECT_INTRO = "[<removed>] "
ENVELOPE_EMAIL_RECIPIENTS = ['<removed>']

## disqus
DISQUS_API_KEY = '<removed>'
DISQUS_WEBSITE_SHORTNAME = '<removed>'

## django sites
SITE_ID = 1

BOOTSTRAP3 = {

    # The URL to the jQuery JavaScript file
    'jquery_url': STATIC_URL+'mysite/js/jquery.min.js',

    # The Bootstrap base URL
    'base_url': STATIC_URL+'mysite/',

    # The complete URL to the Bootstrap CSS file (None means derive it from base_url)
    "css_url": {
        "url": "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css",
        "integrity": "sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u",
        "crossorigin": "anonymous",
    },

    # The complete URL to the Bootstrap CSS file (None means no theme)
    'theme_url': None,

    # The complete URL to the Bootstrap JavaScript file (None means derive it from base_url)
    "javascript_url": {
        "url": "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js",
        "integrity": "sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa",
        "crossorigin": "anonymous",
    },

    # Put JavaScript in the HEAD section of the HTML document (only relevant if you use bootstrap3.html)
    'javascript_in_head': False,

    # Include jQuery with Bootstrap JavaScript (affects django-bootstrap3 template tags)
    'include_jquery': False,

    # Label class to use in horizontal forms
    'horizontal_label_class': 'col-md-3',

    # Field class to use in horizontal forms
    'horizontal_field_class': 'col-md-9',

    # Set HTML required attribute on required fields
    'set_required': True,

    # Set HTML disabled attribute on disabled fields
    'set_disabled': False,

    # Set placeholder attributes to label if no placeholder is provided
    'set_placeholder': True,

    # Class to indicate required (better to set this in your Django form)
    'required_css_class': '',

    # Class to indicate error (better to set this in your Django form)
    'error_css_class': 'has-error',

    # Class to indicate success, meaning the field has valid input (better to set this in your Django form)
    'success_css_class': 'has-success',

    # Renderers (only set these if you have studied the source and understand the inner workings)
    'formset_renderers':{
        'default': 'bootstrap3.renderers.FormsetRenderer',
    },
    'form_renderers': {
        'default': 'bootstrap3.renderers.FormRenderer',
    },
    'field_renderers': {
        'default': 'bootstrap3.renderers.FieldRenderer',
        'inline': 'bootstrap3.renderers.InlineFieldRenderer',
    },
}


try:
    from <removed>.local_settings import *
except ImportError:
    pass
Run Code Online (Sandbox Code Playgroud)

和local_settings.py:

import os
DEBUG = False

STATIC_ROOT = '/home/<removed>/public_html/<removed>/static/'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '<removed>',
        'HOST': 'localhost',
        'USER': '<removed>',
        'PASSWORD': '<removed>',
    }
}

FROM_EMAIL = '<removed>'

BOOTSTRAP3 = {

    # The URL to the jQuery JavaScript file
    'jquery_url': '//code.jquery.com/jquery.min.js',
}

ALLOWED_HOSTS = [
    '<removed>', # Allow domain and subdomains
    '.<removed>.', # Also allow FQDN and subdomains
    '<removed>', # Also allow FQDN and subdomains
]

LOGIN_URL="/__admin/login/"

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': os.path.expanduser('~/logs/django/debug.log'),
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}
Run Code Online (Sandbox Code Playgroud)

Nou*_*olf 0

尝试LOGIN_URL在您的settings.py

LOGIN_URL = '__admin/login'
Run Code Online (Sandbox Code Playgroud)

https://docs.djangoproject.com/en/2.1/ref/settings/#login-url

默认:'/accounts/login/'

重定向请求以进行登录的 URL,尤其是在使用login_required()装饰器时。