Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-admin - how to change formatting of columns - get URLs to display

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.

like image 862
chesco111 Avatar asked May 16 '16 16:05

chesco111


1 Answers

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)
    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
    }
like image 149
ehambright Avatar answered Jan 28 '23 16:01

ehambright