从Django 1.11升级到Django 2.1后的request.POST为空

时间:2018-11-13 16:46:54

标签: python django post url-pattern

本文是对上一个问题的后续报道:

Django request.POST empty

我在Visual Studio 2015上使用Python 3.5.4和Django 1.11.13 启动并运行了一个项目。后来我更新到Django 2.1.2 ,因为我想导入“路径”模块,以便我可以使用它:

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'),

...代替这个:

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' ),

我发现它更易于声明和阅读。进行此更改后,我开始遇到request.POST的问题。我收到“请求”响应,但POST为空,如下所示:

see inspection window with empty request.POST

实际上,我最初并不了解这一点。我花了3天的时间,并与我已恢复的备份副本进行比较,以了解Django版本是不同的。也就是说,我感到困惑的是,较新版本的Django应该无法执行较旧版本的操作,除非发生了我不知道的更改。我只使用Python / Django已有几个月了,有人可以告诉我是否有这个原因吗?我可以在使用Django 2.1.2的path中使用url代替urlpatterns吗?

预先感谢您的帮助。

编辑(11月14日)

这是我的settings.py(我基本上将'app'添加到了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 个答案:

答案 0 :(得分:0)

MIDDLEWARE_CLASSES在Django 1.10和removed in Django 2.0中已弃用。您应该改用MIDDLEWARE

您应该删除SessionAuthenticationMiddleware,因为它hasn't been required since Django 1.10

Django 1.11给出了弃用警告,建议您从MIDDLEWARE切换,但是您一定错过了这一点。在升级Django之前,最好阅读发行说明并修复所有弃用警告。有关更多信息,请参见upgrade guide