request.POST 从 Django 1.11 升级到 Django 2.1 后为空
request.POST empty after upgrading from Django 1.11 to Django 2.1
这个 post 是上一个问题的后续问题:
Django request.POST empty
我有一个项目 运行 Python 3.5.4 和 Django 1.11.13,在 Visual Studio 2015 年。我后来更新到 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 的问题。我收到 "request" 响应,但 POST 为空,如下所示:
其实我一开始并没有意识到这一点。我花了 3 天时间,与我恢复的备份副本进行比较,才意识到 Django 版本不同。也就是说,我感到困惑的是 newer 版本的 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. You should be using MIDDLEWARE
。
您应该删除 SessionAuthenticationMiddleware
因为它 hasn't been required since Django 1.10.
Django 1.11 给出了一个弃用警告,你应该从 MIDDLEWARE
切换,但你一定错过了这个。在升级 Django 之前,最好阅读发行说明并修复所有弃用警告。有关详细信息,请参阅 upgrade guide。
这个 post 是上一个问题的后续问题:
Django request.POST empty
我有一个项目 运行 Python 3.5.4 和 Django 1.11.13,在 Visual Studio 2015 年。我后来更新到 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 的问题。我收到 "request" 响应,但 POST 为空,如下所示:
其实我一开始并没有意识到这一点。我花了 3 天时间,与我恢复的备份副本进行比较,才意识到 Django 版本不同。也就是说,我感到困惑的是 newer 版本的 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. You should be using MIDDLEWARE
。
您应该删除 SessionAuthenticationMiddleware
因为它 hasn't been required since Django 1.10.
Django 1.11 给出了一个弃用警告,你应该从 MIDDLEWARE
切换,但你一定错过了这个。在升级 Django 之前,最好阅读发行说明并修复所有弃用警告。有关详细信息,请参阅 upgrade guide。