Changes between Initial Version and Version 2 of Ticket #9805

12/11/08 19:43:51 (7 years ago)

(Fixed description formatting.)

This isn't really fixing the problem, so much as hiding it. Loading the middlewares should not require access to SCRIPT_NAME (it can change between requests and the middleware is only loaded once). So the code you give actually contains a small bug -- probably not observable for something like the "logout" view, but a bug nonetheless, as it could reverse to a different URL for a different request.

There's a metnion in another ticket that we might add a kind of "lazy" reverse() that resolves upon usage, rather than declaration. When I find that ticket, I'll drop in a comment here and wontfix this one. Leaving open for now to remind me to hunt down the other case.


  • Ticket #9805

    • Property Patch needs improvement unset
    • Property Needs tests unset
    • Property Summary changed from reverse() does not add SCRIPT_NAME to the returned URL if called from modules that contain middleware to reverse() does not add SCRIPT_NAME to the returned URL if called from middleware modules
    • Property Owner changed from nobody to mtredinnick
    • Property Needs documentation unset
  • Ticket #9805 – Description

    initial v2  
    11For example, take the following file, with one example middleware thing that redirects you to the login page if you are not logged in:
    3 {{{login_url=reverse('login')
    57class LoginMiddleware():
    79                if(request.user.is_anonymous() and request.path!=login_url):
    810                        #force login
    9                         return HttpResponseRedirect(login_url+'?next='+request.path)}}}
     11                        return HttpResponseRedirect(login_url+'?next='+request.path)
    1114In this case, login_url will be correct except for the fact that it will be missing the SCRIPT_NAME. Putting the reverse() call inside the process_request function solves the problem, but it makes no sense at all to call that reverse function for every request when it can be called once at server startup.
    1620if self._request_middleware is None:
    1721   self.load_middleware()
Back to Top