我有一个堆叠内联显示。 Inline类的模型有一个ManyToMany关系的子对象。我想显示图像,但我看不到如何阻止django逃离html。好像我需要一个类似于“display_as”的函数,但是如何让django收集所有可用图像并将其显示在“checkboxSelectMultiple”中。在django管理控制台中,如何阻止字段集字段转义HTML?
仅供参考:我想在给他们展示之后再为图片添加一些排序。
Models.py
class BlogWidgetCarousel(models.Model):
entry = models.TextField()
blog = models.ForeignKey(Blog, blank=True, null=True)
position = models.PositiveSmallIntegerField("Position")
images = models.ManyToManyField("Image")
class Meta:
ordering = ('position',)
def __str__(self):
return str(self.position)
def save(self, *args, **kwargs):
self.entry = "<b><i>TODO: create image slider</i></b>"
super(BlogWidgetCarousel, self).save(*args, **kwargs)
def display(self):
return self.entry
class Image(models.Model):
title = models.CharField(max_length=60, blank=False, null=False)
image = models.ImageField(upload_to="images/")
def thumb(self):
return '<a href="{0}"><img src="{0}"></a>'.\
format(MEDIA_URL + str(self.image))
def __str__(self):
#return self.title
#return '<img src="{0}">'.format(MEDIA_URL + str(self.image))
return mark_safe("<b>BOLD</b>") #Added just to test escaping... bold tags still appear on page.
__str__.allow_tags = True #does not appear to work
admin.py
class BlogWidgetCarouselInline(admin.StackedInline):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}
model = BlogWidgetCarousel
extra = 0
#django knows images is ManyToMany
fieldsets = (
("Create Carousel:", {
'fields': (("position"), 'images',)
}),
("Result:", {
'fields': ('thumb', 'display_as',)
}),
)
readonly_fields = ('display_as', 'thumb',)
def display_as(self, instance):
return instance.display()
display_as.allow_tags = True
def thumb(self, instance):
x = ""
for i in instance.images.all():
x += i.thumb()
return x
thumb.allow_tags = True
更新: 我发现的是,插件我使用有以下行渲染功能:
return format_html('<label{}>{} {}</label>',
label_for, self.tag(attrs), self.choice_label)
这意味着模板使用的值已经被转义。为改变这样解决问题:
return format_html(
'<label{}>{} {}</label>', label_for, self.tag(attrs), mark_safe(self.choice_label)
)
现在我不知道如果我在执行中的“不正确”的方式,或者如果它是正常的需要编写自定义窗口小部件,并覆盖渲染功能的东西。
我已更新我的代码以反映您的建议。我仍然无法让模板跳过转义。看起来有些东西正在改变字符串,导致它失去其“安全”状态。 –
您应该在模板中开启'autoescape'。把这个模板标签块中需要转义的代码。这应该可以解决你的问题。更新了ans以包含此内容。 –