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

标签 python django post url-pattern

这篇文章是上一个问题的后续:

Django request.POST empty

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

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 的 urlpatterns 来使用 path 而不是 url 吗?

预先感谢您的帮助。

编辑(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']))

最佳答案

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了解更多信息。

关于python - 从 Django 1.11 升级到 Django 2.1 后 request.POST 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285783/

相关文章:

django - 在virtualenv下设置DJANGO_SETTINGS_MODULE?

html - 表格enctype "application/json"可用吗?

javascript - 发布请求帮助 Angular2

Python:跨文件 block 边界的正则表达式匹配

python - pandas 在创建面板时保留数据类型

python - 高质量、简单的随 secret 码生成器

python - Django 数据库缓存

python - 使用 Django 对 SQLite 执行原始 SQL 结果为 `DatabaseError: near "?": syntax error`

php - 获取选择框选项值不起作用

python - pip 安装 MySQLdb 库时出错