我试图将django silk添加到我的项目中,并希望为我的应用程序中的每个已记录请求添加用户ID。
这就是我设置中间件的方式:
MIDDLEWARE_CLASSES = (
'logger.middleware.LoggerMiddleware',
'silk.middleware.SilkyMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
'middleware.loginrequired.LoginRequiredMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
正如你所看到的,丝绸上面只有一个中间件。此中间件只是将用户ID添加到http标头:
class LoggerMiddleware:
""".
"""
def process_request(self, request):
session_key = request.COOKIES.get('sessionid')
print session_key
if session_key:
try:
session = Session.objects.get(session_key=session_key)
uid = session.get_decoded().get('_auth_user_id')
print uid
request.META['HTTP_USER_ID'] = str(uid)
except Exception as inst:
print "the exception: "
print inst
问题在于每次我尝试发布请求时都会出现异常:
Silk middleware has not been installed correctly. Ordering must ensure that Silk middleware can execute process_request and process_response. If an earlier middleware returns from either of these methods, Silk will not have the chance to inspect the request/response objects.
有谁能告诉我我做错了什么,以及是否有更简单的方法记录在丝绸上提出要求的用户?
答案 0 :(得分:-1)
在MIDDLEWARE_CLASSES元组的最后一个中添加丝绸中间件。