chrome v13 + double-running middleware (django 1.3)

50 views
Skip to first unread message

mcrk

unread,
Aug 22, 2011, 1:17:09 PM8/22/11
to Django users
Hi everyone!

I've created a simple middleware for mobile detection and when testing
values in dev console I came up with some pretty strange behavior.
Let's say, I have this simple middleware class:

class MobileRedirect(object):
def process_request(self, request):
print "watchdog"
return None

and of course it's loaded in my settings:

'django.contrib.messages.middleware.MessageMiddleware',
'middleware.mobile_redirect.MobileRedirect',

and when I go into my login page, this is the results I get in
Firefox:

watchdog
[22/Aug/2011 19:08:02] "GET /login/ HTTP/1.1" 200 742
[22/Aug/2011 19:08:02] "GET /static/css/base.css HTTP/1.1"
[22/Aug/2011 19:08:02] "GET /static/css/fonts/OpenSans/Ope
/1.1" 304 0

and this is using Chrome v13:

watchdog
[22/Aug/2011 19:09:41] "GET /login/ HTTP/1.1" 200 742
[22/Aug/2011 19:09:41] "GET /static/css/base.css HTTP/1.1" 2
[22/Aug/2011 19:09:41] "GET /static/css/fonts/OpenSans/OpenS
/1.1" 304 0
watchdog

"watchdog" is printed out twice, when using chrome!! Using IE gives
the same result as FF.. so how on earth is chrome running the
middleware twice??

Can anyone please make a similar test and let me know, if You get the
same problem.. or just tell me, if I'm doing smth wrong here. I'm new
to django..

Thanks in advance

Daniel Roseman

unread,
Aug 22, 2011, 3:23:15 PM8/22/11
to django...@googlegroups.com
Chrome is probably making a request for /favicon.ico, which is being routed through your middleware.
--
DR. 

Yeled Nova

unread,
Aug 22, 2011, 7:18:41 PM8/22/11
to Django users
Instead of printing out Watchdog, you can "print request.path" to see
which two requests are triggering the middleware.

When you know which additional request is causing the problem, you can
use a decorator to filter it. Here is what I do:

---------------------------------------
In middleware.py:

Class MyMiddleWare:
@forNonStaticRequest
def process_request(self, request):
# Your code goes here
pass
---------------------------------------


And, somewhere else in your project:
===========================================================================
from functools import wraps
from YOURPROJECTNAME.settings import DEBUG

def __forNonStaticRequest(func):
@wraps(func)
def wrapper(SELF, request):
prefix = ('/js/', '/images/', '/upload/', '/style/', '/
__debug__/')
if request.path.startswith(prefix):
return None
return func(SELF, request)
return wrapper

forNonStaticRequest = __forNonStaticRequest if DEBUG else (lambda u:
u)
===========================================================================

What these codes do is to prevent static files request from triggering
the middleware *only* when you're running development server.
You need to properly handle static files when you actually deploy them
in production environment.

I think this will help you.
Reply all
Reply to author
Forward
0 new messages