Changes between Initial Version and Version 2 of Ticket #9805


Ignore:
Timestamp:
Dec 11, 2008, 7:43:51 PM (18 years ago)
Author:
Malcolm Tredinnick
Comment:

(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.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #9805

    • Property Summary reverse() does not add SCRIPT_NAME to the returned URL if called from modules that contain middleware → reverse() does not add SCRIPT_NAME to the returned URL if called from middleware modules
    • Property Owner changed from nobody to Malcolm Tredinnick
  • Ticket #9805 – Description

    initial v2  
    11For example, take the following middleware.py file, with one example middleware thing that redirects you to the login page if you are not logged in:
    22
    3 {{{login_url=reverse('login')
     3{{{
     4#!python
     5login_url=reverse('login')
    46
    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)
     12}}}
    1013
    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.
    … …  
    1417
    1518{{{
     19#!python
    1620if self._request_middleware is None:
    1721   self.load_middleware()
Back to Top