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

1

8

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.

Exciting answered 16/5, 2016 at 16:24 Comment(1)
Can you elaborate what you need? See stackoverflow.com/help/mcveBrillatsavarin
R
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
    }
Rivulet answered 13/7, 2016 at 17:55 Comment(3)
How do I import Markup?Russ
@Daniel - It's from flask import Markup.Antiseptic
Now its, from markupsafe import MarkupUninterrupted

© 2022 - 2024 — McMap. All rights reserved.