7

Question on flask-admin. I setup flask-admin and one of the models i created is pulling urls and url titles from a mysql database. Using flask-admin, how to i get flask-admin to render the urls instead of just text? So make it easier for users to just click on the link from the flask-admin app. Thank you.

I found something on the flask-admin site called column formatters, but not sure how to implement. Does anyone have an example they can share? Thanks.

chesco111
  • 71
  • 1
  • 2

1 Answers1

21

Assuming you have a view named MyView, and your model has fields url, and urltitle then below would work.

from flask import Markup

class MyView(ModelView)
    @staticmethod
    def _user_formatter(view, context, model, name):
        if model.url:
           markupstring = "<a href='%s'>%s</a>" % (model.url, model.urltitle)
           return Markup(markupstring)
        else:
           return ""

    column_formatters = {
        'url': _user_formatter
    }
jackotonye
  • 3,537
  • 23
  • 31
ehambright
  • 1,416
  • 19
  • 27