背景:
环境如题:win + python3 + flask + jinja。
初学python,在做一个博客工具练习flask,sql目前只有入门知识。
目标是想在发布文章的时候,支持markdown。
问题:标题/加粗等都没有问题,但是在插入图片/超链接的时候,拼接的sql语句会被文章(已经被转化为html)中的引号等符号破坏导致报错。
我做的尝试:
xxx.replace(""", "\"").replace("\\", "\"),实测并没有用。
使用HTMLParser的时候,发现依赖的markupbase包没有(pip3 install 提示没找到,豆瓣源)。
想请教的是:
是否需要把网页存到数据库中去?(或者只存markdown文本到数据库,展示的时候再渲染?)假如不应该,有什么好办法么?
假如应该存数据库,有什么办法吧html代码转化成sql安全?比如还有需要替换的字符我没有注意到?或者有现成的库?
You can view my answers to similar questions Flask implements article editing interface
Flask
already has a plug-in for processing MD,Flask-Pagedown
. However, you can also just use themardown
library. For two questions:Flask
已经有了处理MD的插件,Flask-Pagedown
。不过也可以只使用mardown
这个库。对于两个问题:是否需要把网页存到数据库中去?(或者只存markdown文本到数据库,展示的时候再渲染?)假如不应该,有什么好办法么?
在
Flask
作者写的书《Flask Web开发:基于Python的Web应用开发实战》中,是将HTML和markdown文本都保存在数据库中,浏览的时候直接取HTML,编辑的时候取markdown,将markdown保存到数据库的时候随便解析为HTML也保存假如应该存数据库,有什么办法吧html代码转化成sql安全?比如还有需要替换的字符我没有注意到?或者有现成的库?
如果使用
markdown
In the book "Flask Web Development: Practical Web Application Development Based on Python" written by the author of
Flask
, both HTML and markdown text are saved in the database, and the HTML is fetched directly when browsing. Take markdown when editing, and when saving markdown to the database, parse it into HTML and save it as wellIf it should be stored in the database, is there any way to convert the html code into sql safely? For example, are there any characters that need to be replaced that I didn't notice? Or is there a ready-made library?
#🎜🎜# #🎜🎜# #🎜🎜#If using plug-in: #🎜🎜#If you use the
markdown
library, call it directly:The markdown text is stored in the database, and the server implements a filter in jinja2 to render HTML and display markdown. The filter I defined is as follows:
Use: