gpt4 book ai didi

python - django 管理员 : how to make a readonly url field clickable in change_form. html?

转载 作者:IT老高 更新时间:2023-10-28 21:12:06 25 4
gpt4 key购买 nike

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

最佳答案

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

Ref the doc ,readonly_fields 现在也支持这些自定义方式,就像 the link发表在评论中:

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

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

关于python - django 管理员 : how to make a readonly url field clickable in change_form. html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5072189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com