I often find it so that on a new resource I get lost in a sea of different functions and settings, and in order not to lose sight of something interesting, I open / duplicate a tab. This is not always convenient, and the “back” button sometimes works unpredictably, for example, in connection with push history, or because of the POST handler (which is broken) on the redirect (not evil, but for the sake of experiment). Therefore, in order not to irritate the user, I decided to make recent pages. The story about it under the cut.
Option One
Initially, I wanted to implement this function through Middleware, but I ran into a problem: remembering the url and putting it in the cache is not a problem, but getting the current title of the page is yes. And I still think that the only right way to do this is to use middleware. Stupid solutions: use
process_response () and parsit title; use a client-side ajax script to get the title and everything similar to that. Therefore, you need to somehow get to the template engine from middleware and get the root
{% block title%} there . I would be glad if you share an idea on this.
Option Two
Use templatetag. This is exactly how I did it, but the essential disadvantage of this option is that you need to edit the templates with your hands, or think how to embed this tag in base.html.
')
Option Three
Use ajax script a la statistics tracker. The advantage of this option is that the results can be used for different needs, for example, for statistics. Cons - extra script, extra processors, extra data.
Title tag
If you use ajax requests to the same view, then you should add the
request.is_ajax () check to not save the broken links to the user.
@register.simple_tag(takes_context=True, name='save_visited') def save_visited(context, title, **kwargs): request = context['request'] # store last five pages identify_by = get_user_identification(request) visited_pages = cache.get('visited_pages_%s' % identify_by, [])[:4] # context.use_tz - why None if set True in settings? # may be we should save datetime.now() and convert it in getter? now = timezone.now() bundle = force_unicode(title), request.get_full_path()[:255], now visited_pages.insert(0, bundle) cache.set('visited_pages_%s' % identify_by, visited_pages, 60*60*24) return title
Using:
{% extends 'forum/themes/default/edit_profile.html' %} {% load last_visited i18n %} {% block title %}{% save_visited _("Profile setup") %}{% endblock %}
The tag getter, unloads the list in the specified context variable
@register.simple_tag(takes_context=True, name='load_visited') def load_visited(context, variable, **kwargs): identify_by = get_user_identification(context['request']) context[variable] = cache.get('visited_pages_%s' % identify_by, []) return u''
Using:
{% load_visited 'visited' %} {% for title, uri, dt in visited %} <a href="{{ uri }}">{{ title }} {% trans "at" %} {{ dt }}</a><br/> {% endfor %}
For experiments
gist . And yet, the middleware option is haunting me.