26๐
โ
You can create a function clickable_site_domain()
which returns a HTML link as per the value of site_domain
. Then you need to add this method name to the ModelAdmin.list_display
attribute. Finally, you need to mark the string safe to avoid HTML escaping with mark_safe
.
Prior to Django 1.9, you would need to set allow_tags=True
for this function to avoid HTML escaping. (Docs)
from django.utils.text import mark_safe # Older versions
from django.utils.html import mark_safe # Newer versions
class MyModelAdmin(admin.ModelAdmin):
list_display = (..,'clickable_site_domain', ..) # add the custom method to the list of fields to be displayed.
def clickable_site_domain(self, obj):
# return HTML link that will not be escaped
return mark_safe(
'<a href="%s">%s</a>' % (obj.site_domain, obj.site_domain)
)
๐คRahul Gupta
11๐
A more recent answer as of 2019 in django 2.2
from django.contrib import admin
from django.utils.html import format_html
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = [..., 'custom_field', ...]
def custom_field(self, obj):
return format_html('<a href={}>URL</a>', obj.url)
๐คrowman
-4๐
Should be doable in ModelAdmin.
Edit: See this section for how options are defined in the ModelAdmin, and then just do that appropriately when you register in admin.py, making sure your template exists, so for example:
from django.contrib import admin
from .models import MyModel
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
change_list_template = 'myapp/mymodel_change_list.html'
๐คJmills
- Django form with just a BooleanField
- Django and Custom Form validation
- What's equivalent to Django's auto_now, auto_now_add in SQLAlchemy?
- Curl-like tool for wsgi over unix domain socket
- Python โ ERROR โ failed to write data to stream: <open file '<stdout>', mode 'w' at 0x104c8f150>
Source:stackexchange.com