[Answer]-Trying to make a Django / Mezzanine "Page last updated by on date"

1👍

I’ve found a way to do this:

I created a custom tag, saved in /appname/templatetags/appname_tags.py, code as follows:

from django import template
from django.db import connection

register = template.Library()

@register.inclusion_tag(‘appname/_updated_by.html')
def updated_by(pageid):
    cursor = connection.cursor()

    cursor.execute('select user_id from django_admin_log where object_id = %s order by id desc limit 1', [pageid])
    userid_set = cursor.fetchone()

    userid = userid_set[0]

    cursor.execute('select first_name, last_name from auth_user where id = %s', [userid])
    username = cursor.fetchone()

    return {'first_name': username[0],'last_name': username[1]}

This tag gets rendered via /appname/templates/appname/_updated_by.html, which simply contains:

{{ first_name }} {{ last_name }}

The whole thing then gets called from base.html via this code:

{% load appname_tags %}

And then add at the appropriate point:

Page Last Updated: {{ page.updated|date:"d/n/Y" }} by {% updated_by page.id %}

Hope this helps somebody, I’d be interested to hear if there’s an easier / more elegant way to do this!

0👍

You should use the LogEntry model[1].

I would probably make a custom context processor[2] that get’s the request’s
Page, finds the newest LogEntry for the Page instance and adds updated_on
and updated_by context variables.

[1] https://github.com/django/django/blob/master/django/contrib/admin/models.py#L27

[2] https://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

Leave a comment