Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showing the color in the list #8

Open
julianofischer opened this issue Jan 22, 2014 · 5 comments
Open

Showing the color in the list #8

julianofischer opened this issue Jan 22, 2014 · 5 comments
Labels

Comments

@julianofischer
Copy link

Hi,
I created a model with a RGBField.
Then I setted my model admin list_display to: ('color')
where color is my RGBField.

In the list of registered models, the rgb value for the selected color is shown.
Would be amazing if the view was something like the widget.

I do not know if it can be done directly in the django-colorful but if it is possible, it will be pretty cool.

@charettes
Copy link
Owner

I guess both a display helper and list filter could be added. While I find time to work on this you can use the following snippet.

def display_color(obj):
    return '<span style="width:5px;height:5px;color:%s"></span>' % obj.color
display_color.short_description = 'Color'
display_color.allow_tags = True

Add add it as a callable (pass the object directly) to your list_display.

@julianofischer
Copy link
Author

Hi Simon,
I used this snippet and worked perfectly for me.

Thank you very much.

@kirpit
Copy link

kirpit commented Mar 20, 2015

That didn't show up for me in Grappelli admin fyi. Solved it like this (and actually I liked it better):

def display_color(self):
    return '<span style="font-weight:bold;color:{color}">{color}</span>'.format(
        color=self.color,
    )
display_color.short_description = _('Color')
display_color.allow_tags = True

@Proper-Job
Copy link

You should probably be using django.utils.html.format_html to format the string.

@onny
Copy link

onny commented Nov 13, 2023

This worked worked for me on Django 4.2:

from django.utils.html import format_html

[...]
    def display_color(self):
        return format_html('<span style="width:15px;height:15px;display:block;background-color:{}"></span>', self.color)
    display_color.short_description = 'Color'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants