bj13
bj13

Reputation: 35

How to set display text filter in django-admin list_display?

I have a field that contains markdown. When I display it in my templates I can just put in {{activity.activity_notes|markdown}} to get it to format correctly. When it shows up in the Django admin, though, it is unfiltered and doesn't look nice.

I wrote a custom function to return compiled markdown, but when outputting it in Django admin it shows the literal html, tags and all. Is there a way I can set the output filter for a field in the Django admin interface?

Upvotes: 0

Views: 1095

Answers (1)

singer
singer

Reputation: 2636

Just add get_markedown_activity_notes method to your model and use it in admin list_display The method will look like this

from django.contrib.markup.templatetags import markdown

class Activity(models.Model):
    ...
    def get_markedown_activity_notes(self):
        return markdown(self.activity_notes)

#in admin.py

class ActivityAdmin(admin.ModelAdmin):
      list_display = ('id','get_markedown_activity_notes',)

For more use cases see documentation

Upvotes: 2

Related Questions