【发布时间】:2019-04-13 03:27:00
【问题描述】:
这篇文章是上一个问题的后续:
我在 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 为空,如下所示:
实际上,我最初并没有意识到这一点。我花了 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']))
【问题讨论】:
-
我不确定您为什么认为这与路径与 url 有关。当然 Django 2.1 肯定可以接收 POST 参数,如果不能,它作为 web 框架就没有多大用处了。因此,问题肯定出在其他地方。一个可能的罪魁祸首是中间件。你能发布你的中间件设置吗?
-
request.data的内容是什么? -
感谢您的快速回复。你是对的,我认为这与
url和path没有任何关系。只是path没有与 Django 1.11 一起使用,所以我更新到 Django 2.1 才能使用它。但是,显然,作为副作用,request.POST开始显示为空。但我同意你的看法,问题一定出在其他地方,这似乎不对。我是新手,所以我可能搞砸了一些配置选项。我将编辑原始帖子以添加我的settings.py,请在那里查找。至于我的request.data,我没有这个元素(请看上图)。
标签: python django post url-pattern