Run url_for() inside {{ | safe }} template variable

Viewed 21

Short Question: I've got a flask web application that runs a blog website. Is it possible, to run a url_for() function inside a rendered database call result {{ post.content | safe }}?

A bit longer: post.content has html markup saved in my Post database model, which includes all sorts of html tags. {{ post.content | safe }} ensures that the html is properly rendered by the browser. Without | safe the browser displays the markup user-unfriendly as text.

I would like to run a url_for() function inside my database content. Currently, that does not work for me, the url does not resolve and every a tag has {{ url_for() }} in the href like so <a href="{{ url_for() }}>link</a>"

<!-- THIS IS MY HTML template-->
{% extends "layout.html" %}
{% block content %}
<div class="article">
 <!-- POST -->
 {{ post.content | safe }}
 <!-- END POST -->
</div>
{% endblock content %}

And the site database contains something like this for post.content (obiously, longer with more markup):

<!-- INSIDE post.content -->
<p>Text</p>
{{ url_for('static', filename='somefile.jpg') }}

And the resulting website page source is:

[...]
<div class="article">
 <!-- POST -->
<p>Text</p>
{{ url_for('static', filename='somefile.jpg') }}
 <!-- END POST -->
</div>
[...]

Is there some way, to run url_for() nested inside {{ post.content | safe }} and display the proper path instead the string for the function call?

0 Answers
Related