Inserting images using Flask-FlastPages

最后都变了- 提交于 2019-12-12 00:14:36

问题


I have created my blog using Flask-FlatPages and the posts are in Markdown, the challenge I'm having is inserting images in my blog post. The traditional way of inserting images in markdown is not working.

I also tried this without success:

![image]({{ url_for('static', filename="img/my_image.jpg") }})

回答1:


Here is a quick fix! let's say we have a images folder, and we want to use the images/flower.jpg

Step 1: Put the images folder in the static folder.

Step 2: In the text, link the image with ../static/images/flower.jpg.

for example:

![flower](../static/images/flower.jpg)

or

<img src="../static/images/flower.jpg" alt="flower">



回答2:


Here is what worked for me. It turns out that FLATPAGES_HTML_RENDERER is needed to achieve this goal.

Here is the code:

def my_renderer(text):
    prerendered_body = render_template_string(text)
    return pygmented_markdown(prerendered_body)

app = Flask(__name__)
app.config['FLATPAGES_HTML_RENDERER'] = my_renderer
pages = FlatPages(app)

This is also discussed on this post: https://github.com/SimonSapin/Flask-FlatPages/pull/1




回答3:


There are nothing wrong during the markdown to html conversion process.

I think it because bleach.clean() function.

You can try to add to allowed_tags.

allowed_tags = ['img']
allowed_attrs = {'img':['src','alt']};
target.body_html = bleach.linkify(bleach.clean(markdown(value,output_format='html'),tags=allowed_tags,attributes=allowed_attrs,strip=True));


来源:https://stackoverflow.com/questions/30835901/inserting-images-using-flask-flastpages

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