django管理中的自定义html字段changelist_view

发布于 2024-09-25 01:40:04 字数 730 浏览 5 评论 0原文

我想使用 django admin 进行一些小的自定义 - 特别是 changelist_view

class FeatureAdmin(admin.ModelAdmin):
    list_display = (
        'content_object_change_url',
        'content_object',
        'content_type',
        'active',
        'ordering',
        'is_published',
    )

    list_editable = (
       'active',
       'ordering',
    )

    list_display_links = (
        'content_object_change_url',
    )

admin.site.register(get_model('features', 'feature'), FeatureAdmin)

这个想法是 'content_object_change_url' 可以是到另一个对象的 change_view 的链接...方便管理员用户快速直接导航到该项目。

对于此类事情,我的另一种情况是添加外部源的链接或图像字段的缩略图。

我原以为我听说过“插入 html”选项——但也许我有点言过其实了。

感谢您的帮助!

I'd like to some little customisation with the django admin -- particularly the changelist_view

class FeatureAdmin(admin.ModelAdmin):
    list_display = (
        'content_object_change_url',
        'content_object',
        'content_type',
        'active',
        'ordering',
        'is_published',
    )

    list_editable = (
       'active',
       'ordering',
    )

    list_display_links = (
        'content_object_change_url',
    )

admin.site.register(get_model('features', 'feature'), FeatureAdmin)

The idea is that the 'content_object_change_url' could be a link to another object's change_view... a convenience for the admin user to quickly navigate directly to the item.

The other case I'd have for this kind of thing is adding links to external sources, or thumbnails of image fields.

I had thought I'd heard of a 'insert html' option -- but maybe I'm getting ahead of myself.

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2024-10-02 01:40:04

您可以在 FeatureAdmin 类上提供一个自定义方法,该方法返回 content_object_change_url 的 HTML:

class FeatureAdmin(admin.ModelAdmin):

    [...]

    def content_object_change_url(self, obj):
        return '<a href="%s">Click to change</a>' % obj.get_absolute_url()
    content_object_change_url.allow_tags=True

请参阅 文档

You can provide a custom method on the FeatureAdmin class which returns HTML for content_object_change_url:

class FeatureAdmin(admin.ModelAdmin):

    [...]

    def content_object_change_url(self, obj):
        return '<a href="%s">Click to change</a>' % obj.get_absolute_url()
    content_object_change_url.allow_tags=True

See the documentation.

海拔太高太耀眼 2024-10-02 01:40:04

注意并使用 format_html (请参阅文档此处)作为 mark_safe util 已自 1.10 版本起已弃用。此外,自版本 1.11 起,将删除对 ModelAdmin 方法上的 allowed_tags 属性的支持。

from django.utils.html import format_html
from django.contrib import admin

class FeatureAdmin(admin.ModelAdmin):
    list_display = (
        'change_url',
        [...]
    )
    def change_url(self, obj):
        return format_html('<a target="_blank" href="{}">Change</a>', obj.get_absolute_url())
    change_url.short_description='URL'

Pay attention and use format_html (See docs here) as the mark_safe util has been deprecated since version 1.10. Moreover, support for the allow_tags attribute on ModelAdmin methods will be removed since version 1.11.

from django.utils.html import format_html
from django.contrib import admin

class FeatureAdmin(admin.ModelAdmin):
    list_display = (
        'change_url',
        [...]
    )
    def change_url(self, obj):
        return format_html('<a target="_blank" href="{}">Change</a>', obj.get_absolute_url())
    change_url.short_description='URL'
此刻的回忆 2024-10-02 01:40:04

我花了两个小时才找出为什么丹尼尔·罗斯曼的解决方案对我不起作用。尽管他是对的,但有一个例外:当您想在Admin中创建自定义计算字段(只读)时。这行不通。非常简单的解决方案(但很难找到)是在特殊的构造函数中返回字符串:SafeText()。也许这与 Django 2 或 readonly_fields (其行为与经典字段不同)相关。

下面是一个工作示例,可以工作,但如果没有 SafeText() 则无法实现:

from django.utils.safestring import SafeText

class ModelAdminWithData(admin.ModelAdmin):

    def decrypt_bin_as_json(self, obj):
        if not obj:
            return _("Mode insert, nothing to display")
        if not obj.data:
            return _("No data in the game yet")
        total = '<br/><pre>{}</pre>'.format(
            json.dumps(json.loads(obj.data),
                       indent=4).replace(' ', ' '))
        return SafeText(total)  # !! working solution !! <------------------

    decrypt_bin_as_json.short_description = _("Data")
    decrypt_bin_as_json.allow_tags = True

    readonly_fields = ('decrypt_bin_as_json',)

    fieldsets = (
        (_('Data dump'), {
            'classes': ('collapse',),
            'fields': ('decrypt_bin_as_json',)
        }),
    )

It took me two hours to find out why Daniel Roseman's solution doesn't work for me. Even though he is right, there's one exception: when you want to make custom calculated fields (read only) in the Admin. This wont work. The very easy solution (but hard to find) is to return your string in a special constructor: SafeText(). Maybe this is linked with Django 2 or with readonly_fields (which behave differently from classical fields)

Here's a working sample that works but doesn't without SafeText():

from django.utils.safestring import SafeText

class ModelAdminWithData(admin.ModelAdmin):

    def decrypt_bin_as_json(self, obj):
        if not obj:
            return _("Mode insert, nothing to display")
        if not obj.data:
            return _("No data in the game yet")
        total = '<br/><pre>{}</pre>'.format(
            json.dumps(json.loads(obj.data),
                       indent=4).replace(' ', ' '))
        return SafeText(total)  # !! working solution !! <------------------

    decrypt_bin_as_json.short_description = _("Data")
    decrypt_bin_as_json.allow_tags = True

    readonly_fields = ('decrypt_bin_as_json',)

    fieldsets = (
        (_('Data dump'), {
            'classes': ('collapse',),
            'fields': ('decrypt_bin_as_json',)
        }),
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文