python - Alternative way to return HTML as a read only field on the Django Admin -


i have django admin class contains read field returns table in html 2 different links. first link loads page of payments user has sent, second loads page of payments user has received. in order create functionality, there need return lot of html. there alternative returning it?

here current relevant code:

class useradmin(simplehistoryadmin):     readonly_fields = ('payment_history')      def payment_history(self, obj):         return "<table style='border: none'>" \                "<tr><td><a href='/admin/payment/payment/?sender__id=%s'>sent user</a></td>" \                "<td><a href='/admin/payment/payment/?receiver__id=%s'>sent user</a></td>" \                "</tr></table>" % (obj.id, obj.id)     payment_history.allow_tags = true 

a preferred alternative having code in real html file can returned same method.

how 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 user</a></td>     <td><a href='/admin/payment/payment/?receiver__id={{object.id}}'>sent 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}) 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -