django admin:如何在change_form.html中使只读url字段可点击?

发布于 2024-10-18 18:01:33 字数 140 浏览 0 评论 0原文

我想在change_form 页面的管理中创建一个可点击的只读URL 字段。我尝试了一个小部件,但很快意识到小部件仅适用于表单字段。所以,在我尝试用 jQuery 解决这个问题(查找和替换之类的)之前,我想知道 python 中是否有更优雅的解决方案。有什么想法吗?

I want to make a readonly URL field clickable in the admin on a change_form page. I tried a widget, but soon realized widgets are for form fields only. So, before I try to solve this problem with jQuery (find and replace or something), I would like to know if there is a more elegant solution for this in python. Any ideas?

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

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

发布评论

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

评论(4

浮生面具三千个 2024-10-25 18:01:33

老问题,但仍然值得回答。

参考文档
readonly_fields 现在也支持这些自定义方式,就像 评论中发布的链接

def the_callable(obj):
    return u'<a href="#">link from the callable for {0}</a>'.format(obj)
the_callable.allow_tags = True

class SomeAdmin(admin.ModelAdmin):
    def the_method_in_modeladmin(self, obj):
         return u'<a href="#">link from the method of modeladmin for {0}</a>'.format(obj)
    the_method_in_modeladmin.allow_tags = True

    readonly_fields = (the_callable, 'the_method_in_modeladmin', 'the_callable_on_object')

ObjModel.the_callable_on_object = lambda self, obj: u'<a href="#">link from the callable of the instance </a>'.format(obj)
ObjModel.the_callable_on_object.__func__.allow_tags = True

上面的代码将在其更改表单页面中呈现三个只读字段。

Old question, but still deserves an answer.

Ref the doc,
readonly_fields also supports those customization ways now, works just as the link posted in the comment:

def the_callable(obj):
    return u'<a href="#">link from the callable for {0}</a>'.format(obj)
the_callable.allow_tags = True

class SomeAdmin(admin.ModelAdmin):
    def the_method_in_modeladmin(self, obj):
         return u'<a href="#">link from the method of modeladmin for {0}</a>'.format(obj)
    the_method_in_modeladmin.allow_tags = True

    readonly_fields = (the_callable, 'the_method_in_modeladmin', 'the_callable_on_object')

ObjModel.the_callable_on_object = lambda self, obj: u'<a href="#">link from the callable of the instance </a>'.format(obj)
ObjModel.the_callable_on_object.__func__.allow_tags = True

The above code would render three readonly fields in its change form page then.

我是有多爱你 2024-10-25 18:01:33

更新后的答案可以在这篇文章中找到。

它使用 format_html 实用程序 因为 allow_tags 已被弃用。

还有 ModelAdmin.readonly_fields< 的文档/a> 真的很有帮助。

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

class SomeAdmin(admin.ModelAdmin):
    readonly_fields = ('my_clickable_link',)

    def my_clickable_link(self, instance):
        return format_html(
            '<a href="{0}" target="_blank">{1}</a>',
            instance.<link-field>,
            instance.<link-field>,
        )

    my_clickable_link.short_description = "Click Me"

The updated answer can be found in this post.

It uses the format_html utility because allow_tags has been deprecated.

Also the docs for ModelAdmin.readonly_fields are really helpful.

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

class SomeAdmin(admin.ModelAdmin):
    readonly_fields = ('my_clickable_link',)

    def my_clickable_link(self, instance):
        return format_html(
            '<a href="{0}" target="_blank">{1}</a>',
            instance.<link-field>,
            instance.<link-field>,
        )

    my_clickable_link.short_description = "Click Me"
茶底世界 2024-10-25 18:01:33

我按照 okm 提供的链接进行操作,并设法在更改表单页面中包含一个可点击的链接。

我的解决方案(添加到 admin.ModelAdmin,而不是 models.model)

readonly_fields = ('show_url',)
fields = ('show_url',)

def show_url(self, instance):
    return '<a href="%s">%s</a>' % ('ACTUAL_URL' + CUSTOM_VARIABLE, 'URL_DISPLAY_STRING')
show_url.short_description = 'URL_LABEL'
show_url.allow_tags = True

I followed the link provided by okm and I managed to include a clickable link in change form page.

My solution (Add to admin.ModelAdmin, NOT models.model)

readonly_fields = ('show_url',)
fields = ('show_url',)

def show_url(self, instance):
    return '<a href="%s">%s</a>' % ('ACTUAL_URL' + CUSTOM_VARIABLE, 'URL_DISPLAY_STRING')
show_url.short_description = 'URL_LABEL'
show_url.allow_tags = True
歌入人心 2024-10-25 18:01:33

在管理.py中

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

   class Document(admin.ModelAdmin):
      readonly_fields = ["Document_url"]


      def Document_url(self, obj):
            return format_html("<a target='_blank',href='{url}'>view</a>",url=obj.document_url)

in admin.py

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

   class Document(admin.ModelAdmin):
      readonly_fields = ["Document_url"]


      def Document_url(self, obj):
            return format_html("<a target='_blank',href='{url}'>view</a>",url=obj.document_url)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文