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.


On Aug 23, 1:17 am, mcrk <mari...@poczta.fm> wrote:
> 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

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