how to return clickable link in django 2.0 admin.py file

55 views
Skip to first unread message

arvind yadav

unread,
Jan 31, 2018, 6:55:48 AM1/31/18
to Django users
click.png

Andréas Kühne

unread,
Jan 31, 2018, 7:03:05 AM1/31/18
to django...@googlegroups.com
Hi,

Your question is a bit vague - however, if I do my own interpretation of it, you want to return a link from a custom property in django admin?

You can do that specifically by adding the string to a format_html call. See here : https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
  • from django.db import models
    from django.contrib import admin
    from django.utils.html import format_html
    
    class Person(models.Model):
        first_name = models.CharField(max_length=50)
        last_name = models.CharField(max_length=50)
        color_code = models.CharField(max_length=6)
    
        def colored_name(self):
            return format_html(
                '<span style="color: #{};">{} {}</span>',
                self.color_code,
                self.first_name,
                self.last_name,
            )
    
    class PersonAdmin(admin.ModelAdmin):
        list_display = ('first_name', 'last_name', 'colored_name')
Regards

Andréas

2018-01-31 12:13 GMT+01:00 arvind yadav <developer....@gmail.com>:

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/156eeac6-1c1a-441a-80a3-1eb390e070e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages