How to add clickable links to a field in Django admin?

How to add clickable links to a field in Django admin?

To add clickable links to a field in the Django admin interface, you can use the list_display option in your model's admin class. Here's how you can do it:

Assuming you have a Django model like this:

from django.db import models class MyModel(models.Model): name = models.CharField(max_length=100) website = models.URLField() 

You can create an admin class for this model in your admin.py file and use the list_display option to customize the display of the website field as a clickable link:

from django.contrib import admin from django.utils.html import format_html from .models import MyModel class MyModelAdmin(admin.ModelAdmin): list_display = ('name', 'website_link') # Add 'website_link' to list_display def website_link(self, obj): # Create a clickable link for the 'website' field return format_html('<a href="{}" target="_blank">{}</a>', obj.website, obj.website) website_link.short_description = 'Website Link' # Customize the column header name admin.site.register(MyModel, MyModelAdmin) 

In this code:

  1. We create a custom admin class MyModelAdmin that inherits from admin.ModelAdmin.

  2. We add 'website_link' to the list_display tuple to include the new field in the admin list view.

  3. We define a method website_link that takes an object (obj) as its argument. This method generates an HTML link using the format_html function and the website field of the object. The target="_blank" attribute ensures that the link opens in a new tab.

  4. We set the short_description attribute to customize the column header name in the admin list view.

With this configuration, the website field in the admin list view will display as clickable links, allowing you to navigate to the corresponding websites directly from the Django admin interface.

Examples

  1. "Django admin add clickable links to ForeignKey field"

    • Description: This query is about enhancing the Django admin interface to display ForeignKey fields as clickable links to related objects.
    # models.py from django.db import models class RelatedModel(models.Model): name = models.CharField(max_length=100) class MainModel(models.Model): related_field = models.ForeignKey(RelatedModel, on_delete=models.CASCADE) # admin.py from django.contrib import admin from .models import RelatedModel, MainModel class MainModelAdmin(admin.ModelAdmin): list_display = ['related_field_link'] def related_field_link(self, obj): return '<a href="/admin/app_name/relatedmodel/%s/">%s</a>' % (obj.related_field.id, obj.related_field.name) related_field_link.allow_tags = True related_field_link.short_description = 'Related Field' admin.site.register(RelatedModel) admin.site.register(MainModel, MainModelAdmin) 
  2. "Django admin clickable links in list display"

    • Description: This query refers to making the fields in the list display of Django admin clickable.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['clickable_field'] def clickable_field(self, obj): return '<a href="/admin/app_name/yourmodel/%s/">%s</a>' % (obj.id, obj.field_to_display) clickable_field.allow_tags = True clickable_field.short_description = 'Clickable Field' admin.site.register(YourModel, YourModelAdmin) 
  3. "Django admin make fields clickable"

    • Description: This query is about making various fields in Django admin clickable.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['clickable_field'] def clickable_field(self, obj): return '<a href="/admin/app_name/yourmodel/%s/">%s</a>' % (obj.id, obj.field_to_display) clickable_field.allow_tags = True clickable_field.short_description = 'Clickable Field' admin.site.register(YourModel, YourModelAdmin) 
  4. "Django admin ForeignKey clickable link"

    • Description: This query focuses specifically on making ForeignKey fields clickable in Django admin.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['related_field'] def related_field(self, obj): return '<a href="/admin/app_name/relatedmodel/%s/">%s</a>' % (obj.related_field.id, obj.related_field.name) related_field.allow_tags = True related_field.short_description = 'Related Field' admin.site.register(YourModel, YourModelAdmin) 
  5. "Django admin display clickable links"

    • Description: This query is about displaying clickable links in Django admin.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['clickable_field'] def clickable_field(self, obj): return '<a href="%s">%s</a>' % (obj.url_field, obj.field_to_display) clickable_field.allow_tags = True clickable_field.short_description = 'Clickable Field' admin.site.register(YourModel, YourModelAdmin) 
  6. "Django admin make ForeignKey clickable"

    • Description: This query is about making ForeignKey fields clickable in Django admin.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['related_field'] def related_field(self, obj): return '<a href="%s">%s</a>' % (obj.related_field.get_absolute_url(), obj.related_field.name) related_field.allow_tags = True related_field.short_description = 'Related Field' admin.site.register(YourModel, YourModelAdmin) 
  7. "Django admin hyperlink ForeignKey fields"

    • Description: This query seeks to hyperlink ForeignKey fields in Django admin.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['related_field'] def related_field(self, obj): return '<a href="%s">%s</a>' % (obj.related_field.get_absolute_url(), obj.related_field.name) related_field.allow_tags = True related_field.short_description = 'Related Field' admin.site.register(YourModel, YourModelAdmin) 
  8. "Django admin ForeignKey field clickable"

    • Description: This query relates to making ForeignKey fields clickable in Django admin interface.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['related_field'] def related_field(self, obj): return '<a href="/admin/app_name/relatedmodel/%s/">%s</a>' % (obj.related_field.id, obj.related_field.name) related_field.allow_tags = True related_field.short_description = 'Related Field' admin.site.register(YourModel, YourModelAdmin) 
  9. "Django admin ForeignKey field link"

    • Description: This query is about creating clickable links for ForeignKey fields in Django admin.
    # admin.py from django.contrib import admin from .models import YourModel class YourModelAdmin(admin.ModelAdmin): list_display = ['related_field'] def related_field(self, obj): return '<a href="/admin/app_name/relatedmodel/%s/">%s</a>' % (obj.related_field.id, obj.related_field.name) related_field.allow_tags = True related_field.short_description = 'Related Field' admin.site.register(YourModel, YourModelAdmin) 
  10. "Django admin make ForeignKey fields clickable"


More Tags

tdd image-quality flask-sqlalchemy eval apache-spark-2.0 oracle9i xcode7 git-track servlets treetable

More Python Questions

More Geometry Calculators

More Chemical reactions Calculators

More Fitness Calculators

More Organic chemistry Calculators