title: django url dispatching and middleware
body: <p>In my development system (mac os x) I added the following lines at the end of my urls.py file:</p>
  
  <pre><code>if re.match('darwin',sys.platform):
  # serving media files using the development server
      urlpatterns += patterns('',(r'^site_media/(?P&lt;path&gt;.*)$',
          'django.views.static.serve',
          {'document_root': '/Users/henri/sites_django/wmsproject/wmssite/site_media'}),)
  </code></pre>
  
  <p>in order to serve media files.</p>
  
  <p>Everything goes as expected, well almost...</p>
  
  <p>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:</p>
  
  <pre><code>def process_view(self, request, view_func, view_args, view_kwargs):
      assert False
  </code></pre>
  
  <p>When I enter this url in my browser:</p>
  
  <pre><code>http://localhost:8000/site_media/images/logo_wms_web.gif
  </code></pre>
  
  <p>I see, much to my astonishment, the following information the dump:</p>
  
  <pre><code>self         &lt;wmssite.middleware.LanguageMiddleware.LanguageRedirect instance at 0x10117fe60&gt;
  view_args    {}
  view_func    &lt;function serve at 0x101281578&gt;
  view_kwargs  {'document_root': '/Users/henri/sites_django/wmsproject/wmssite/site_media', 'path': u'images/logo_wms_web.gif'}
  </code></pre>
  
  <p>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.</p>
  
  <p>So it seems I got it wrong. Can somebody explain when exactly Middleware is called in relation to URL dispatching?</p>