简体   繁体   English

Django Silk中间件错误

[英]Django silk middleware error

Im trying to add django silk to my project, and want to add the user ID for each of the logged requests on my app. 我试图将django silk添加到我的项目中,并想为我的应用程序中每个记录的请求添加用户ID。

This is how I have my middleware set: 这是我设置中间件的方式:

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',
)

As you can see, theres only one middleware above silk. 如您所见,丝绸之上只有一种中间件。 This middleware just adds the user ID to an http header: 该中间件仅将用户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

The problem is that every time I try to do a post request I get the exception: 问题是,每次我尝试执行发布请求时,都会收到异常:

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.

Can anyone tell me what Im doing wrong and if there is an easier way to record the user that is making the requests on silk? 谁能告诉我我做错了什么,是否有更简单的方法来记录正在网上发出请求的用户?

在MIDDLEWARE_CLASSES元组的最后一个中添加丝绸中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM