2015-10-06 126 views

回答

6

你可以創建一個函數clickable_site_domain(),根據site_domain的值返回一個HTML鏈接。然後,您需要將此方法名稱添加到ModelAdmin.list_display屬性中。最後,您需要爲此函數設置allow_tags=True以避免HTML轉義。

Django默認會HTML輸出。如果您不希望 轉義該方法的輸出,請爲此方法提供allow_tags 屬性,其值爲True

class MyModelAdmin(admin.ModelAdmin): 

    list_display = (..,'clickable_site_domain', ..) # add the custom method to the list of fields to be displayed. 

    def clickable_site_domain(self, obj): 
     return '<a href="%s">%s</a>' % (obj.site_domain, obj.site_domain)' # return HTML link 

    clickable_site_domain.allow_tags = True # set this to not HTML-escape the output 
+0

ahhh所以關鍵是allow_tags –

+0

'allow_tags'不HTML轉義輸出。 –

+0

@MartinMassera更新了您的修改建議。 –

0

應該是可行的in ModelAdmin

編輯:見this section如何選擇中的ModelAdmin定義,然後只需做適當的時候,你在admin.py註冊,確保你的模板存在,因此,例如:

from django.contrib import admin 
from .models import MyModel 

@admin.register(MyModel) 
class MyModelAdmin(admin.ModelAdmin): 
    change_list_template = 'myapp/mymodel_change_list.html' 
+0

你怎麼樣,能解釋一下嗎? –

+0

新增,但@Rahul Gupta的回答顯然更好,我錯過了這個選項。 – Jmills

+0

謝謝反正! –

相關問題