0
class DirectAdmin(admin.ModelAdmin):
    def order_pdf(obj):
        # return "<a href='{}'>pdf</a>".format(
        url=reverse('orders:admin_order_pdf', args=[obj.id])
        return "http://localhost:8000" + url
    order_pdf.allow_tags = True
    order_pdf.short_description = 'PDF bill'
    list_display=['id','name','price','phone_number',order_pdf]
admin.site.register(Product)
admin.site.register(Category)
admin.site.register(Direct,DirectAdmin)

This is my admin.py. Here in the admin section of my objects I want to display a link where the link should act as an anchor where it should redirect to that particular link in next tab.

But when I run this code I can see the uri.

enter image description here

I want to make that section in my pdf as an anchor which redirects and opens in another tab

Is that possible?

Alasdair
  • 298,606
  • 55
  • 578
  • 516
john
  • 539
  • 2
  • 9
  • 24

1 Answers1

0

Your commented out code is nearly correct. You need to return html (e.g. <a href='{}'>pdf</a>), but you need to mark the output as safe so that it isn't escaped in the template.

You can use format_html for this.

class DirectAdmin(admin.ModelAdmin):
    def order_pdf(obj):
        url=reverse('orders:admin_order_pdf', args=[obj.id])
        return format_html("<a href='{}'>{}</a>", url, "pdf")
    order_pdf.allow_tags = True
    order_pdf.short_description = 'PDF bill'
    list_display=['id','name','price','phone_number',order_pdf]
Alasdair
  • 298,606
  • 55
  • 578
  • 516