消息未出现在 AWS 开发服务器 Django-3.1 的管理员中
messages not appearing in admin on AWS dev server Django-3.1
context:Django 3.1 应用部署到带有 terraform 的 AWS lambda。我还没有设置我的生产设置,这是我的开发服务器。
我正在使用 https://github.com/adamchainz/apig-wsgi
我注意到当试图通过管理员使用 djangorestframework-api-key
获取 api 密钥时,来自 django.contrib.messages
的消息不会出现。它在 lambda 上意味着我无法访问 lambda REPL 以编程方式创建 api 密钥。
注意:django-debug-toolbar
也不起作用,而且很安静,所以我暗暗怀疑我的 context_processors
设置已关闭,所以我不没有 messages
或 DEBUG
变量,但我在我的设置中找不到问题??下面是我没有调试工具栏设置的代码。
project/settings/base.py
DEBUG = True
INSTALLED_APPS = [
'app',
'rest_framework',
'rest_framework_api_key',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'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',
],
},
},
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_STORAGE_BUCKET_NAME= [redacted]
AWS_DEFAULT_ACL = [redacted]
AWS_QUERYSTRING_AUTH= [redacted]
AWS_S3_REGION_NAME = [redacted]
AWS_LOCATION = [redacted]
USE_I18N = True
STATIC_URL = '/static/'
REST_FRAMEWORK = {
...
'DEFAULT_PERMISSION_CLASSES': [
"rest_framework_api_key.permissions.HasAPIKey",
]
}
project/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
application = get_wsgi_application()
lambda_function.py
def install_secrets():
"""Add the secrets from the secret named by ENV_SECRET_ID to os.environ"""
import os
secret_id = os.environ.get('ENV_SECRET_ID')
if not secret_id:
return
import boto3
import json
session = boto3.session.Session()
client = session.client('secretsmanager')
response = client.get_secret_value(SecretId=secret_id)
overlay = json.loads(response['SecretString'])
os.environ.update(overlay)
def manage(event, context):
"""Entry point for running a management command. Supported formats:
- "migrate!"
- ["migrate"]
- {"command": ["migrate"]}
"""
if isinstance(event, dict):
command = event['command']
else:
command = event
if isinstance(command, str):
command = command.split()
install_secrets()
from django.core.wsgi import get_wsgi_application
get_wsgi_application() # Initialize Django
from django.core import management
return management.call_command(*command)
_real_handler = None
def lambda_handler(event, context):
"""Entry point for web requests"""
global _real_handler
if _real_handler is None:
print('No Cache, getting secrets.')
install_secrets()
from apig_wsgi import make_lambda_handler
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
_real_handler = make_lambda_handler(application)
return _real_handler(event, context)
默认管理模板 admin/base.html
我希望在其中显示消息(它只是循环遍历消息对象,不需要任何 JS 或 CSS 来显示):
https://github.com/django/django/blob/2d8232fa716f5fe46eec9f35a0e90c2d0f126279/django/contrib/admin/templates/admin/base.html#L82
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
requirements.txt
Django>=3.1,<3.2
django-storages==1.11.1
apig-wsgi
psycopg2-binary
djangorestframework
djangorestframework-api-key==2.1.0
尝试次数:
- 努力重新加载
F5
等等
- 尝试添加 django 调试工具栏并记录显示在管理员消息中的 api 键,但没有成功,因为工具栏也没有显示(上面的代码中没有显示)
- 尝试在开发服务器上收集静态数据
- 尝试检查用户会话并查看用户会话中的消息,那里有 条消息
一位才华横溢的同事解决了这个问题。添加
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
您的设置解决了问题。
context:Django 3.1 应用部署到带有 terraform 的 AWS lambda。我还没有设置我的生产设置,这是我的开发服务器。
我正在使用 https://github.com/adamchainz/apig-wsgi
我注意到当试图通过管理员使用 djangorestframework-api-key
获取 api 密钥时,来自 django.contrib.messages
的消息不会出现。它在 lambda 上意味着我无法访问 lambda REPL 以编程方式创建 api 密钥。
注意:django-debug-toolbar
也不起作用,而且很安静,所以我暗暗怀疑我的 context_processors
设置已关闭,所以我不没有 messages
或 DEBUG
变量,但我在我的设置中找不到问题??下面是我没有调试工具栏设置的代码。
project/settings/base.py
DEBUG = True
INSTALLED_APPS = [
'app',
'rest_framework',
'rest_framework_api_key',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'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',
],
},
},
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_STORAGE_BUCKET_NAME= [redacted]
AWS_DEFAULT_ACL = [redacted]
AWS_QUERYSTRING_AUTH= [redacted]
AWS_S3_REGION_NAME = [redacted]
AWS_LOCATION = [redacted]
USE_I18N = True
STATIC_URL = '/static/'
REST_FRAMEWORK = {
...
'DEFAULT_PERMISSION_CLASSES': [
"rest_framework_api_key.permissions.HasAPIKey",
]
}
project/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
application = get_wsgi_application()
lambda_function.py
def install_secrets():
"""Add the secrets from the secret named by ENV_SECRET_ID to os.environ"""
import os
secret_id = os.environ.get('ENV_SECRET_ID')
if not secret_id:
return
import boto3
import json
session = boto3.session.Session()
client = session.client('secretsmanager')
response = client.get_secret_value(SecretId=secret_id)
overlay = json.loads(response['SecretString'])
os.environ.update(overlay)
def manage(event, context):
"""Entry point for running a management command. Supported formats:
- "migrate!"
- ["migrate"]
- {"command": ["migrate"]}
"""
if isinstance(event, dict):
command = event['command']
else:
command = event
if isinstance(command, str):
command = command.split()
install_secrets()
from django.core.wsgi import get_wsgi_application
get_wsgi_application() # Initialize Django
from django.core import management
return management.call_command(*command)
_real_handler = None
def lambda_handler(event, context):
"""Entry point for web requests"""
global _real_handler
if _real_handler is None:
print('No Cache, getting secrets.')
install_secrets()
from apig_wsgi import make_lambda_handler
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
_real_handler = make_lambda_handler(application)
return _real_handler(event, context)
默认管理模板 admin/base.html
我希望在其中显示消息(它只是循环遍历消息对象,不需要任何 JS 或 CSS 来显示):
https://github.com/django/django/blob/2d8232fa716f5fe46eec9f35a0e90c2d0f126279/django/contrib/admin/templates/admin/base.html#L82
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
requirements.txt
Django>=3.1,<3.2
django-storages==1.11.1
apig-wsgi
psycopg2-binary
djangorestframework
djangorestframework-api-key==2.1.0
尝试次数:
- 努力重新加载
F5
等等 - 尝试添加 django 调试工具栏并记录显示在管理员消息中的 api 键,但没有成功,因为工具栏也没有显示(上面的代码中没有显示)
- 尝试在开发服务器上收集静态数据
- 尝试检查用户会话并查看用户会话中的消息,那里有 条消息
一位才华横溢的同事解决了这个问题。添加
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
您的设置解决了问题。