Django last visited urls list

1.4k Views Asked by At

I'm writing django-based forum, and i've decided it would be suitable for user to browse his last pages. Also, user tracking middleware can be a good aid for suggestions, and so on.

  1. I think, the easiest way to do it is to use the Django Middleware, but i ran into a problem: how to get the title of the page being rendered? Override process_template_response? Can i get the {% block title %} there?

  2. The second server-side way is to use a template tag, i think. In the easiest case, it should should look like {% block title %}{% last_visited _("Page title") %}{% endblock %}.

  3. The third, stupid way: make an ajax script, that will push current user's opened page with title into his session. So, this method will just avoid us getting the page title.

I think, the right way is to get the title block from a template context in middleware. How can i do it?

Thanks.

UPDATE

Made the gist with realisation of the second method using templates and django.cache. The simplest usage:

{% block title %}{% save_visited _("Profile setup") %}{% endblock %}
...
{% load_visited 'visited' %}
{% for title, uri, dt in visited %}
    <a href="{{ uri }}">{{ title }} {% trans "at" %} {{ dt }}</a><br/>
{% endfor %}

Also, i'm still looking for a method allows to get the page's {% block title %} in the middleware. Of course, i can use, i.e., lxml parser and get the title in the process_response method, but it is an ugly overkill.

Thanks for any advice.

1

There are 1 best solutions below

1
On

In your case i would use the second approach slightly modified:

{% last_visited  current_page_title  current_page_url  num %}

which would do two things:

  1. Store the current title and url in a session variable (list of last visited pages)
  2. Render the num last visited pages from the same session variable