Back to Home

Implement last visited / recent pages in Django

django framework · django templates · recent pages

Implement last visited / recent pages in Django

    It often happens to me 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, due to push history, or because of a POST handler that broke on a redirect (not for evil, but for the sake of experiment). Therefore, in order not to annoy 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 to get the current title of the page - yes. And I still think that the only right way to implement this is to use middleware. Wacky solutions: use process_response () and parse title; use ajax-script on the client side to get the title and all the same. Therefore, you need to somehow get to the template engine from middleware and get the root {% block title%} there . I would be glad to share an idea on this subject.

    Second option


    Use templatetag. This is exactly how I did it, but the significant minus of this option is that you need to edit templates with your hands, or think about how to embed this tag in base.html.

    Third option


    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 handlers, extra data.




    Tag to save title

    If you use ajax-requests to the same view, then you should add the request.is_ajax () check so as not to save 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 %}
    


    Getter tag, unloads the list into 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 %}
        {{ title }} {% trans "at" %} {{ dt }}
    {% endfor %}


    For experiments gist . And yet, the middleware option haunts me.

    Read Next