Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change row colour in Django Admin List

I want to highlight rows (set the backgorund colour) in the Django Admin page (change_list.html) based on a field in the model called status. What is the best way to do this?

I have 3 statuses. Open, Active, Closed. I'd like rows with open to be green, active to be orange and closed to be red.

I've found documentation about changing the templates so but not sure how to check the status to colour the row.

like image 245
John Avatar asked Oct 25 '10 13:10

John


People also ask

How do I change the admin color in Django?

To change the color scheme of the Django admin panel we need to override the base admin template then include CSS rules to replace the original styling. Now cd into that directory and create another called admin and inside admin create a base. html file.

Can we customize Django admin panel?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

Can we change Django admin theme?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

How do I change the admin heading in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.


1 Answers

I was wrong, there's another alternative using just the Django admin app.

In the admin.py for your app, you can define a renderer for the contents of a table cell. Here's the variant for my film library:

class FilmAdmin(admin.ModelAdmin):

   def film_status(self, obj):
        if obj.status() != 'active':
            return '<div style="width:100%%; height:100%%; background-color:orange;">%s</div>' % obj.status()
        return obj.status()
    film_status.allow_tags = True

    list_display = ('id', 'title', 'film_status')

admin.site.register(Film, FilmAdmin)

Here, I've created a field name, film_status, that does not exist in the Film model, and defined it as a method of FilmAdmin. It gets passed the item for every row. I've had to tell the renderer to allow_tags, which tells the admin app not to "safe" the HTML content.

This won't fill the whole cell, though, as the cell itself has some padding. Only the part of the cell your app is allowed to fill (as defined by the admin's stylesheet) will be filled. But it's good enough for my purposes.

There you go. Two completely different, but useful, techniques for decorating the content of a cell in a Django admin list.

like image 57
Elf Sternberg Avatar answered Oct 05 '22 04:10

Elf Sternberg