We recently uncovered a bug in our code, which was pretty confusing,
and is explainable only via the fine-print of
http://docs.djangoproject.com/en/dev/topics/http/middleware/ -- and
reading through the Django source.  So I thought I'd share, and maybe
a Django developer might comment on whether the current behaviour is
really desirable, and if so perhaps consider improving the docs.

We have some Middleware that updates the user's session, from a
process_response method.  Sometimes this would fail, because the
request object had no session.

It turns out this would happen because the Common middleware had
issued a redirect (specifically, APPEND_SLASH was redirecting us to
correct the view's URL).

In a case like this, the current behaviour is not like the 'onion' as
illustrated in the docs, because whilst the redirect meant that *only*
the Common middleware had its process_request function executed, every
loaded middleware's process_response is executed.  (This is mentioned
in the docs, it turns out, in the process_request section but not in
the process_response section.)

What this means is that even though it seems, and is the case 99% of
the time, that middleware listed later can rely totally on middleware
further up the list (i.e. the 'onion' model), this is in fact not the
case, for processing responses.  So response processing code is either
subject to rare bugs, or has to contain extra checks to make sure of
its preconditions (in our case a check for hasattr(request,
'session') ).

Personally I think this is confusing enough that it would be better if
the program logic could be changed, so that later-listed middleware
would definitely only either run, or not run, depending on whether the
earlier middleware allows the request to proceed or not.  But I guess
such a change could break existing code, so at the very least the docs
could use a bit more explanation and or highlighting in bold!

thanks

George Lund


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to