Kathiravan Natarajan
Kathiravan Natarajan

Reputation: 3508

Rendering template in Flask admin view prints the text instead of html functionality

I have formatted the one of a column in the Flask admin view(app.py).

class main_coursesView(ModelView):
    def _user_formatter(view, context, model, name):
       return render_template("template.html")

    column_formatters = {
        'image': _user_formatter
    }

And the template.html file contains the following code.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<a href="#" id="upload_widget_opener">Upload images</a>
<script src="https://widget.cloudinary.com/global/all.js" type="text/javascript"></script>  

<script type="text/javascript">  
  document.getElementById("upload_widget_opener").addEventListener("click", function() {
    cloudinary.openUploadWidget({ cloud_name: 'zeboio', sources: [ 'local', 'url', 'camera', 'image_search', 
                 'facebook', 'dropbox', 'google_photos' ], upload_preset: 'mdjqdwsf'}, 
      function(error, result) { console.log(error, result) });
  }, false);
</script>

</body>
</html>

So when I run the view(app.py), in the formatted column the contents of the html file is getting printed. Instead, I need to perform the event mentioned in the html file.

Upvotes: 0

Views: 756

Answers (1)

pjcunningham
pjcunningham

Reputation: 8046

Use the Markup method from markupsafe to wrap the render_template output.

from markupsafe import Markup

class main_coursesView(ModelView):
    def _user_formatter(view, context, model, name):
       return Markup(render_template("template.html"))

    column_formatters = {
        'image': _user_formatter
    }

Upvotes: 3

Related Questions