RequestStack middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class RequestStackMiddleware(object):
    '''
    Keeps track of the last 3 succesful requests
    '''
    def process_response(self, request, response):
        if 'requeststack' not in request.session:
            request.session['requeststack'] = ['/', '/', request.path]
        else:
            if request.method == 'GET' and 'text/html' in response.headers['Content-Type']:
                stack = request.session['requeststack']
                stack = stack[1:] # remove the first item
                stack.append(request.path)
                request.session['requeststack'] = stack
        
        return response

In a view:
    return HttpResponseRedirect(request.session['requeststack'][-1]) # or -2 or -3

More like this

  1. Middleware to detect visitors who arrived from a search engine by exogen 5 years, 1 month ago
  2. create and authenticate an anonymous user by chr15m 2 years, 8 months ago
  3. Redirect Multiple Domains to a Single Domain by rpoulton 4 years, 7 months ago
  4. SSL Middleware by sjzabel 5 years, 2 months ago
  5. FirstRun Middleware by TheMysteriousX 1 year ago

Comments

(Forgotten your password?)