Y
Y
Yupiter75752021-05-08 19:45:58
Flask
Yupiter7575, 2021-05-08 19:45:58

How to translate a string in html flask?

Let's say there are two pages. The first, static, contains a form that is designed to write html files to the database. Here is how the file is processed:

def OneFile(path):
    with io.open(path, 'r') as f:
        content = f.read()
    soup = bs(content, features='lxml')
    content = str(soup.find('body'))[6:-7]
    images = soup.find_all('img')
    if images != []:
        images = list(map(str, list(images)))
        for i in images:
            img = bs(i, features='lxml').find('body').find('img')
            src = img.get('src')
            src2 = f"{{{{ url_for('static' filename='{str(src)}') }}}}"
            img2 = str(img).replace(str(src), str(src2))
            content = content.replace(str(img), img2)
    return content

Everything works great, only there was a problem with the second page.
It is dynamic, and it shows what the function above returns. Actually, it's not hard to guess that instead of html tags, something like this is shown: <h1></h1>.
I solved this problem on the front, namely, I wrote a js script that turns these characters above into html tags. So what is the question, the url_for function does not work because no one told the Python interpreter that these characters are html. How to make it understand and jinja work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-05-08
@yupiter7575

To prevent flask from doing this, you need to use the safe filter.
{{ url_for('static' filename='...')|safe }}
See also the documentation .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question