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

和自甴很熟 提交于 2019-12-07 07:32:40

问题


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.


回答1:


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
    }


来源:https://stackoverflow.com/questions/37258668/flask-admin-how-to-change-formatting-of-columns-get-urls-to-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!