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

标签 python django django-admin

我想在 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/

相关文章:

python - 在 Pandas 中使用自定义偏移量别名

javascript - 如何获取 django View 的 url 字符串?

python - 在 Django 管理员中嵌套内联?

使用自定义模型 User 进行 Django 身份验证

python - 使用正则表达式分组将字符串转换为字典

python - 寻找 numpy 模式向量

python - debian 8.2 中的 "pip is configured with locations that require TLS/SSL"

python - Django 按日期分组和每个类别的计数

python - Django ORM 获取字段的最大值和对应的其他列值

Django OneToOneField 和管理 UI