request.POST empty after upgrading from Django 1.1

2019-03-04 02:57发布

问题:

This post is a follow-up of this previous question:

Django request.POST empty

I had a project up and running with Python 3.5.4 and Django 1.11.13, on Visual Studio 2015. I later updated to Django 2.1.2 because I wanted to import the "path" module, so that I can use this:

urlpatterns = [

    path ( '',                                c_views.Indice,              name = 'indice' ),
    path ( '<int:CompiladoID>',               c_views.Detalle,             name = 'detalle'),
    path ( 'elementos/<int:CompiladoID>',     c_views.Elementos,           name = 'elementos'),
    path ( 'datoselementos/<int:ElementoID>', c_views.DatosElemento,       name = 'datoselemento'),

...instead of this:

urlpatterns = [

    url ( r'^$',                                    c_views.Indice,         name = 'indice'),
    url ( r'^(?P<CompiladoID>\d+)/$',               c_views.Detalle,        name = 'detalle' ),
    url ( r'^(?P<CompiladoID>\d+)/elementos$',      c_views.Elementos,      name = 'elementos' ),
    url ( r'^(?P<CompiladoID>\d+)/generar$',        c_views.Generar,        name = 'generar' ),

which I find easier to declare and read. After this change I started to experience problems with request.POST. I got a "request" response, but POST was empty, as shown here:

Actually, I was not initially aware of this. It's taken me 3 days, and a compare with a backup copy that I've recovered, to realize that Django version was different. That said, I'm puzzled about the fact that a newer version of Django should not be able to do something that older versions did, unless something has changed that I don't know of. I've only been working with Python/Django for a couple of months, can anyone tell me if there is a reason to this? Can I not use path instead of url for my urlpatterns using Django 2.1.2?

Thank you in advance for your help.

EDIT (november 14th)

This is my settings.py (I basically added 'app' to INSTALLED_APPS):

"""
Django settings for MemoProject project.

Generated by 'django-admin startproject' using Django 1.9.1.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
import posixpath

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '---'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'app',
    # Add your apps here to enable them
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    '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',
]

ROOT_URLCONF = 'MemoProject.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'MemoProject.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'),
    }
}


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


# 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


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

STATIC_URL = '/static/'

STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static']))

回答1:

MIDDLEWARE_CLASSES was deprecated in Django 1.10 and removed in Django 2.0. You should be using MIDDLEWARE instead.

You should remove SessionAuthenticationMiddleware because it hasn't been required since Django 1.10.

Django 1.11 gives a deprecation warning that you should switch from MIDDLEWARE, but you must have missed this. Before upgrading Django, it's good practice to read the release notes, and fix any deprecation warnings. See the upgrade guide for more info.