[Fixed]-Alternative way to return HTML as a read only field on the Django Admin

1👍

How about using render_to_string? https://docs.djangoproject.com/en/1.9/topics/templates/#django.template.loader.render_to_string

templates/myapp/payment_history.html:

<table style='border: none'>
  <tr>
     <td><a href='/admin/payment/payment/?sender__id={{object.id}}'>Sent by this user</a></td>
    <td><a href='/admin/payment/payment/?receiver__id={{object.id}}'>Sent to this user</a></td>
  </tr>
</table>

admin.py:

from django.template.loader import render_to_string

class UserAdmin(SimpleHistoryAdmin):
    readonly_fields = ('payment_history')

    def payment_history(self, obj):
    return render_to_string('myapp/payment_history.html', {'object':obj})
👤raphv

Leave a comment