简体   繁体   中英

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.

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:

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元组的最后一个中添加丝绸中间件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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