Naeem Khan
Naeem Khan

Reputation: 960

Django: Get page title from middleware

I have a small middleware I wrote to keep track of user activity:

class AccessLogs(object):

def __init__(self, get_response):
    self.get_response = get_response

def __call__(self, request):
    response = self.get_response(request)

    if "/media/" not in request.path:
        try:
            ActivityLog(user=request.user, pageURL=request.path).save()
        except Exception as e:
            print(e)

    return response

Is there any way I can get the title of the page using this method of middleware? I have looked up a LOT of stuff here like templateview, custom response but nothing seems to be working. Is there any class or function that retrieves the visited page's title? Any help would be really appreciated.

EDIT: What I'm trying to look for is a method to get the title of the page user has just visited, so I can store it with other information in the database with it in this middleware.

Upvotes: 1

Views: 445

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

Yes, although not all responses are HTTP responses, nor do all HTTP responses have per se a title. But we can try to do a best effort to obtain the title from the response.

In order to do this, we can use a HTML scraper, like beautifulsoup4 [PiPy]. You might need to install:

pip install beautifulsoup4 lxml

We can then aim to obtain the title from a response with:

from bs4 import BeautifulSoup

def get_response_title(response):
    try:
        soup = BeautifulSoup(response.content, 'lxml')
        return soup.find('title').getText()
    except AttributeError:
        return None

You can thus use this in your middleware like:

class AccessLogs(object):

    def __call__(self, request):
        response = self.get_response(request)
        if '/media/' not in request.path:
            try:
                title = get_response_title(response)
                ActivityLog(user=request.user, title=title, pageURL=request.path).save()
            except Exception as e:
                print(e)

That being said, as @IainShelvington says, it will slow down processing, since we each time will take a look at the response. Some web development frameworks like Yesod [yesodweb.com] will set the title as a variable that is passed in the handler, and thus make it more convenient to detect that.

Upvotes: 2

Related Questions