Reputation: 59313
I have this class to configure django.contrib.admin
for my Stream
model.
class StreamAdmin(admin.ModelAdmin):
def stream_title(instance):
return instance.info.title
def network_name(instance):
return '<img src="%s" />' % instance.network.image_url
fieldsets = (
(None, {
'fields' : ('name', 'network', 'score', 'custom_url')
}),
)
list_display = ('name', stream_title, network_name, 'online', 'score')
ordering = ('name', 'score')
The network_name function returns HTML, but it's escaped. How can I stop that?
Upvotes: 1
Views: 1442
Reputation: 599450
Use allow_tags
.
def network_name(instance):
return '<img src="%s" />' % instance.network.image_url
network_name.allow_tags = True
Upvotes: 5
Reputation: 2368
You can use mark_safe:
from django.utils.safestring import mark_safe
class StreamAdmin(admin.ModelAdmin):
...
def network_name(instance):
return mark_safe('<img src="%s" />' % instance.network.image_url)
...
Try using safe filter at the template or even autoescape.
Using the autoescape tag you can set up a block like this:
{% autoescape on %}
{{ model.network_name }}
{% endautoescape %}
<!-- or either -->
{{ model.network_name|safe }}
Upvotes: 2