2016-07-06 5 views
0

2つの異なるリンクを持つHTMLテーブルを返す読み取り専用フィールドを含むDjango管理クラスがあります。最初のリンクは、ユーザーが送信したすべての支払いのページをロードし、2番目のリンクは、ユーザーが受け取ったすべての支払いのページをロードします。この機能を作成するには、多くのHTMLを返す必要があります。それを返すだけの代替手段はありますか?Django管理者の読み取り専用フィールドとしてHTMLを返す代わりの方法

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 by this user</a></td>" \ 
       "<td><a href='/admin/payment/payment/?receiver__id=%s'>Sent to this user</a></td>" \ 
       "</tr></table>" % (obj.id, obj.id) 
    payment_history.allow_tags = True 

好ましい代替は、同じ方法で返すことができ、実際のHTMLファイルでこのコードを持つことになります。

は、ここに私の現在の関連するコードです。

答えて

1

render_to_stringの使用はいかがですか? https://docs.djangoproject.com/en/1.9/topics/templates/#django.template.loader.render_to_string

テンプレート/ 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}) 
関連する問題