0

Middleware-Wizards,

I was wondering if there is a more elegant way to find out whether a request serves one of my custom view methods or rather static content etc. instead?

class MyMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        if view_func.__name__ != 'serve':
            # do something here
        return None
pasql
  • 3,815
  • 5
  • 23
  • 33
  • 1
    I'm curious what you're trying to do - `serve()` isn't meant to be used in production, so I don't see why this middleware would be useful. – Alasdair Aug 30 '15 at 21:14
  • Ah that's true! I forgot about that :) What I basically want to achieve is to create a log entry for every page that is visited in order to better understand how people use the website and improve the website based on the results :) – pasql Aug 30 '15 at 21:22
  • I think it'll be better to use some javascript for it, so you won't slow down all requests. – beezz Aug 30 '15 at 21:24
  • @beezz: interesting, I'll give that a try as well :) – pasql Aug 30 '15 at 21:52

1 Answers1

3

If you are serving static files only via STATIC_URL you can check request.path. (process_request)

def process_request(request):
    if request.path.startswith(settings.STATIC_URL):
        # do something here

Anyway that's applicable only during development cause usually apache or nginx or some CDN is serving static files and those requests will never hit your django app.

beezz
  • 2,398
  • 19
  • 15