views:

31

answers:

1

Hi,

In my development system (mac os x) I added the following lines at the end of my urls.py file:

if re.match('darwin',sys.platform):
# serving media files using the development server
    urlpatterns += patterns('',(r'^site_media/(?P<path>.*)$',
        'django.views.static.serve',
        {'document_root': '/Users/henri/sites_django/wmsproject/wmssite/site_media'}),)

in order to serve media files.

Everything goes as expected, well almost...

I included a Middleware class to intercept processing before my views are called. In this middleware I defined a process_view function. Things are not working as expected so I inserted an 'assert False' as the first line of this function, as follows:

def process_view(self, request, view_func, view_args, view_kwargs):
    assert False

When I enter this url in my browser:

http://localhost:8000/site_media/images/logo_wms_web.gif

I see, much to my astonishment, the following information the dump:

self         <wmssite.middleware.LanguageMiddleware.LanguageRedirect instance at 0x10117fe60>
view_args    {}
view_func    <function serve at 0x101281578>
view_kwargs  {'document_root': '/Users/henri/sites_django/wmsproject/wmssite/site_media', 'path': u'images/logo_wms_web.gif'}

The parameters I see in the dump are exactly the parameters you seen in the urls.py file I just showed. URL dispatching happens before views are called (obviously) but I also thought that Middleware was called AFTER url dispatching and before calling views. But this looks like the Middleware is called before URL dispatching.

So it seems I got it wrong. Can somebody explain when exactly Middleware is called in relation to URL dispatching?

+1  A: 

What in the traceback leads you to the conclusion that the middleware is being called before URL dispatching? On the contrary, it is clear that it is being called after that, and before calling the view, as it clearly has the name of the view it is being despatched to - serve, in other words django.views.static.serve as defined in your urls.py.

Daniel Roseman
Yes you are right, however it still doesn't explain why the middleware is intercepting the media file request. One addition. I just copied the whole project to a Virtual Machine running Apache, mod_python. When I do the same things in this VM, everything behaves as expected. Request on /site_media/..... etc doesn't get intercepted by the middleware. So it seems there is only a problem when using the development server.
Henri
I still can't understand why you think this is a problem, or why you even are surprised by this. You've set up a view to serve media, routed by your urls.py. So of course it will follow the full Django stack. On your production hardware, you're not using this url or view, so it doesn't go through the middleware. This is exactly what you should be expecting.
Daniel Roseman
Well believe it or not your answers have been very helpful. You were right, it is no problem at all. Thanks for your replies.
Henri