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?
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.
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" 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With