Django admin TabularInline - 是否有一个很好的方法来添加一个自定义的html列? [英] Django admin TabularInline - is there a good way of adding a custom html column?
问题描述
我有一个模型( Entry
),其中包含一个简单的属性:
code> @property
def image(self):
return str(self.id)+_+ self.round.season.name +_+ self.round.theme + _+ self.person.name
我使用它来构建特定图像文件的名称磁盘。所以我知道将在 / path / to / images /(model.image())中的图像。jpg
我可以在管理页面上的TabularInline布局中显示原始图像属性,方法是将其添加到readonly_fields集合,但是如何获取一个包含模型属性的自定义HTML的列? p>
eg
< img src =/ images / {{model .image}}。jpg/>
你可以做的是在你的 TabularInline
返回所需HTML的子类,然后在中使用该方法的名称代替
c image
从django .utils.safestring import mark_safe
class ImageInline(admin.TabularInline):
...
fields =(...,'render_image')
def render_image(self,obj):
return mark_safe(< img src =/ images /%s.jpg/>%obj.image)
I've got a model (Entry
) which contains a simple property:
@property
def image(self):
return str(self.id)+"_"+self.round.season.name+"_"+self.round.theme+"_"+self.person.name
I use this to build the name of a particular image file on disk. So I know that there's going to be an image at /path/to/images/(model.image()).jpg
I can display the raw image property itself within the TabularInline layout on an admin page by adding it to the readonly_fields collection, but how would I go about getting a column which had custom html wrapped around the model property?
e.g.
<img src="/images/{{model.image}}.jpg" />
What you can do is create a method in your TabularInline
subclass that returns the HTML you want, then use that method's name in place of image
in ImageInline
.fields
:
from django.utils.safestring import mark_safe
class ImageInline(admin.TabularInline):
...
fields = (..., 'render_image')
def render_image(self, obj):
return mark_safe("""<img src="/images/%s.jpg" />""" % obj.image)
这篇关于Django admin TabularInline - 是否有一个很好的方法来添加一个自定义的html列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!