Django:从中间件获取页面标题

Django: Get page title from middleware

我写了一个小中间件来跟踪用户 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

请问有什么办法可以通过这种中间件的方式获取到页面的标题吗?我在这里查找了很多东西,比如模板视图、自定义响应,但似乎没有任何效果。是否有任何 class 或函数可以检索已访问页面的标题?非常感谢任何帮助。

编辑:我正在寻找的是一种获取用户刚刚访问过的页面标题的方法,因此我可以将它与数据库中的其他信息一起存储在这个中间件中。

,虽然不是所有的响应都是 HTTP 响应,也不是所有的 HTTP 响应都有本身标题。但是我们可以尝试尽最大努力从响应中获取标题。

为此,我们可以使用 HTML 抓取工具,例如 beautifulsoup4 [PiPy]。您可能需要安装:

pip install <b>beautifulsoup4 lxml</b>

然后我们可以从以下响应中获取标题:

from bs4 import BeautifulSoup

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

因此您可以在您的中间件中使用它,例如:

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)

也就是说,@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] 会将标题设置为在处理程序中传递的变量,从而更方便地检测它。