2

I am running a set of kiosks that each display content from my Django app. Each kiosk pings a url like myurl.com/location=downtown every minute to see if there are any updates.

My models.py looks something like this. Every time the model is pinged, the last_updated is updated.

class Location(models.Model):
    name = models.CharField(max_length=20)
    last_update = models.DateTimeField()

Unfortunately, every so often, the browser in the kiosk crashes. What I am looking to do is add to the front of the admin interface, after I log in, a little green or red light to indicate if it has been more than 5 minutes since one of the locations had been updated.

Is this the best way to go about making a heart beat for the application? How do I modify the admin interface to give me the status lights to see if the kiosks are ok?

Alexis
  • 23,545
  • 19
  • 104
  • 143

3 Answers3

3

Oops, I typed up the following before re-reading the question. So, if you would like to have a custom column on the list display for Location, do the following. Otherwise, look at @jpic's answer.

--

Yep, you can define custom Admin columns with a method on a ModelAdmin like so:

import datetime
from django.contrib import admin
from my_app.models import Location

class LocationAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug', 'custom_column',)

    # define the row x column value here
    def custom_column(self, obj):
        if obj.last_updated < datetime.datetime.now() - datetime.timedelta(minutes=5):
            retval = ('red.jpg', 'This location checked in more than 5 minutes')
        else
            retval = ('green.jpg', 'This location checked in less than 5 minutes ago')
        return "<img src='%s' alt='%s' />" % retval

    # set the column heading here
    custom_column.short_description = 'Status'

# don't forget to register the newly sub-classed ModelAdmin
admin.site.register(Location, LocationAdmin)

The custom method can also be an attribute on the Model itself. For details, see the admin docs.

jnovinger
  • 117
  • 4
  • With the new version of Django import `html` from `django.utils` then use `return html.format_html("%s" % retval)` to display HTML – Florent Jan 16 '20 at 13:55
2
  1. Make a custom template tag that displays the light if a kiosk was updated

  2. Override the admin base template, ie. copy django/contrib/admin/templates/admin/base_site.html to your_project/templates/admin/base_site.html

  3. Add your template tag in the admin base template you have copied

This is some work but hopefully everything you need is documented.

jpic
  • 32,891
  • 5
  • 112
  • 113
0

It has been a while since I worked in Django, so I can't help you with the code. But your problem seems like it should be easy in Django.

The admin interface is sort of magical, and I don't think you should try to hack it to add a status light. (It's possible that they have changed things since I looked at Django and maybe it's not so magical now?)

But it should be very easy to make a new page that shows status, and make the page only available to an admin. You can get a plugin for Firefox or Chrome that auto-refreshes a page, which will cause Django to re-run the queries and re-build the page. Then just leave that page open and refreshing while you do admin stuff in the admin interface.

steveha
  • 74,789
  • 21
  • 92
  • 117
  • Page auto-refresh might be annoying if the user is currently reading a list of editing a model in a form... – jpic Jun 12 '12 at 02:28
  • I was suggesting auto-refresh only on a specific page, with its own URL, that shows only status information. A read-only page, not part of the admin interface. – steveha Jun 12 '12 at 02:47