简体   繁体   English

Flask 博客文章广泛的控件 - 如何控制文本格式、添加链接、图片等的能力

[英]Flask blog post extensive controls - How to control text formating, ability to add links, pictures etc

I am building a website-blog using the Flask python web-framework.我正在使用 Flask python 网络框架构建一个网站博客。 I can create a blog post, wherein the content field is where all the text of my post goes.我可以创建一篇博客文章,其中content字段是我文章的所有文本所在的位置。 The problem is it is just a TextAreaField , with raw unformatted text.问题是它只是一个TextAreaField ,带有原始未格式化的文本。 In every major forum I've seen (or even this stack-overflow post) while you're assembling your post you're given the ability to have bold text, italicized text, add links, add code-styled text, add pictures, add numbered lists, headings and more.在我见过的每个主要论坛(甚至是这个堆栈溢出帖子)中,当您组装帖子时,您都可以使用粗体文本、斜体文本、添加链接、添加代码样式文本、添加图片、添加编号列表、标题等。

How can I add any of that to my post?我如何将其中的任何内容添加到我的帖子中? Visually what I'm going for are these controls:从视觉上看,我要的是这些控件:

在此处输入图片说明

There's the ability to make a section of the text bold, another to make it italic, underline it, add a picture, add a link, add a code section, add a heading (these would be enough for my needs; also embedding a video would be nice).可以将一部分文本设为粗体,另一部分设为斜体、下划线、添加图片、添加链接、添加代码部分、添加标题(这些足以满足我的需要;还可以嵌入视频会好的)。 I understand that an image is possibly a more difficult matter as I run into the problem of where to save the images.我知道图像可能是一个更困难的问题,因为我遇到了在哪里保存图像的问题。 Save them all per-post, save them independently in another table in the database and link them to an individual post (one-post to many-pictures relationship), store them to an external service (say Imgur)?将它们全部保存在每个帖子中,将它们独立保存在数据库中的另一个表中并将它们链接到单个帖子(一个帖子到多张图片的关系),将它们存储到外部服务(比如 Imgur)?

In short my PostModel table (using flask_sqlalchemy ) contains these columns: title , content , author , datePosted and tags .简而言之,我的PostModel表(使用flask_sqlalchemy )包含以flask_sqlalchemytitlecontentauthordatePostedtags My PostForm form contains the fields: title ( StringField ), content ( TextAreaField ) and tags ( FieldList(FormField()) ) and a button submitField .我的PostForm表单包含字段: titleStringField )、 contentTextAreaField )和tagsFieldList(FormField()) )和一个按钮submitField

How do I accomondate these functions in my flask web-app?我如何在我的 Flask 网络应用程序中使用这些功能? Is it maybe possible that I can format the text in the content TextAreaField such that it is displayed a certain way?是否有可能我可以格式化内容TextAreaField的文本,使其以某种方式显示? Hopefully I made myself clear.希望我说清楚了。 Thanks in advance.提前致谢。

What I needed was a WYSIWYG (What You See Is What You Get) editor.我需要的是 WYSIWYG(所见即所得)编辑器。 There are many freely available with popular ones being TinyMCE, Summernote, CKEditor and others.有许多免费提供,流行的有 TinyMCE、Summernote、CKEditor 等。

Your current code requires few modifications.您当前的代码几乎不需要修改。 It is not that hard to integrate it with your code.将它与您的代码集成并不难。 You specify the required libraries and the initialization code.您指定所需的库和初始化代码。

Typically you have a content field of type textarea which will require a class attribute as specified for the library (for Summernote say class="summernoteClass" ) such that the textarea element can be targeted by the library's javascript code.通常,您有一个textarea类型的content字段,它需要为库指定的class属性(对于 Summernote 说class="summernoteClass" ),以便textarea元素可以由库的 javascript 代码定位。

Then you simply store this content to the database as before.然后您只需像以前一样将此内容存储到数据库中。 Note that this may require cleaning the content in case the user types any malicious <script> s (if so I recommend the bleach python module and then bleach.clean(str) ).请注意,这可能需要清理内容,以防用户键入任何恶意的<script> s(如果是这样,我建议使用bleach python 模块,然后是bleach.clean(str) )。

Finally to display the html back (and not the html entities) using Jinja2 template framework you can do so by simply appending |safe in the html code, like so: <p class="article-content">{{ post.content | safe }}</p>最后,要使用 Jinja2 模板框架显示 html(而不是 html 实体),您只需在 html 代码中添加|safe即可,如下所示: <p class="article-content">{{ post.content | safe }}</p> <p class="article-content">{{ post.content | safe }}</p> . <p class="article-content">{{ post.content | safe }}</p> Maybe this will help others.也许这会帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM