Reputation: 875
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?
Upvotes: 1
Views: 1462
Reputation: 599460
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.
Upvotes: 2