如何向Django admin中的字段添加可单击的链接?

时间:2022-09-12 08:52:14

I have this admin.py

我有这个admin.py

class LawyerAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Name',   {'fields': ['last', 'first', 'firm_name', 'firm_url', 'school', 'year_graduated']}),
    ]
    list_display = ('last', 'first', 'school', 'year_graduated', 'firm_name', 'firm_url')
    list_filter = ['school', 'year_graduated']
    search_fields = ['last', 'school', 'firm_name']

and I want to make "firm_url" fields clickable with each of the url listed in the field. How can I do this? Thank you.

我想让“固件url”字段可以点击字段中列出的每个url。我该怎么做呢?谢谢你!

4 个解决方案

#1


58  

Define a custom method in your LawyerAdmin class that returns the link as HTML:

在LawyerAdmin类中定义一个自定义方法,该方法将链接返回为HTML:

def show_firm_url(self, obj):
    return '<a href="%s">%s</a>' % (obj.firm_url, obj.firm_url)
show_firm_url.allow_tags = True

See the documentation.

见文档。

#2


50  

Use the format_html utility. This will escape any html from parameters and mark the string as safe to use in templates. The allow_tags method attribute has been deprecated in Django 1.9.

使用format_html效用。这将从参数中退出任何html,并将字符串标记为安全的在模板中使用。在Django 1.9中,已经废弃了allow_tags方法属性。

from django.utils.html import format_html

class LawyerAdmin(admin.ModelAdmin):
    list_display = ['show_firm_url', ...]
    ...

    def show_firm_url(self, obj):
        return format_html("<a href='{url}'>{url}</a>", url=obj.firm_url)

    show_firm_url.short_description = "Firm URL"

Now your admin users are safe even in the case of:

现在你的行政用户是安全的,即使在以下情况:

firm_url == 'http://a.aa/<script>eval(...);</script>'

See the documentation for more info.

有关更多信息,请参见文档。

#3


5  

add show_firm_url to list_display

添加show_firm_url按照list_display

#4


2  

But it overrides the text display specified in my models and displays "show firm url" on the head of the column

但它会覆盖我的模型中指定的文本显示,并在列的顶部显示“show firm url”

You can change it by assigning short_description property:

您可以通过分配short_description属性来更改它:

show_firm_url.short_description = "Firm URL"

#1


58  

Define a custom method in your LawyerAdmin class that returns the link as HTML:

在LawyerAdmin类中定义一个自定义方法,该方法将链接返回为HTML:

def show_firm_url(self, obj):
    return '<a href="%s">%s</a>' % (obj.firm_url, obj.firm_url)
show_firm_url.allow_tags = True

See the documentation.

见文档。

#2


50  

Use the format_html utility. This will escape any html from parameters and mark the string as safe to use in templates. The allow_tags method attribute has been deprecated in Django 1.9.

使用format_html效用。这将从参数中退出任何html,并将字符串标记为安全的在模板中使用。在Django 1.9中,已经废弃了allow_tags方法属性。

from django.utils.html import format_html

class LawyerAdmin(admin.ModelAdmin):
    list_display = ['show_firm_url', ...]
    ...

    def show_firm_url(self, obj):
        return format_html("<a href='{url}'>{url}</a>", url=obj.firm_url)

    show_firm_url.short_description = "Firm URL"

Now your admin users are safe even in the case of:

现在你的行政用户是安全的,即使在以下情况:

firm_url == 'http://a.aa/<script>eval(...);</script>'

See the documentation for more info.

有关更多信息,请参见文档。

#3


5  

add show_firm_url to list_display

添加show_firm_url按照list_display

#4


2  

But it overrides the text display specified in my models and displays "show firm url" on the head of the column

但它会覆盖我的模型中指定的文本显示,并在列的顶部显示“show firm url”

You can change it by assigning short_description property:

您可以通过分配short_description属性来更改它:

show_firm_url.short_description = "Firm URL"