Flask中不出现的锡

2024-06-10 10:05:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我非常仔细地遵循了这个指南:https://www.kevin7.net/post_detail/2但似乎无法让TinyMCE在我的Flask应用程序中工作

本质上,它根本不会在屏幕上呈现,好像我错过了什么

这是我在html模板中使用的代码。我试图让它从createpost表单中呈现contenttext区域字段

  <h1> TEST </h1>
  <script type="textarea/javascript" src="{{ url_for('static', filename='tinymce/tinymce.min.js') }}"></script>
  <script type="textarea/javascript">
      tinymce.init({
      selector: '#content',
      plugins: [
        'advlist autolink link image imagetools lists charmap print preview hr anchor pagebreak spellchecker',
        'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking',
        'save table contextmenu directionality template paste textcolor codesample'
      ],
      toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons | codesample',
      codesample_languages: [
        {text: 'HTML/XML', value: 'markup'},
        {text: 'JavaScript', value: 'javascript'},
        {text: 'CSS', value: 'css'},
        {text: 'Processing', value: 'processing'},
        {text: 'Python', value: 'python'}
      ]
      });
  </script>

下面是我试图将其附加到的PostForm。内容字段

class PostForm(FlaskForm):
    title = StringField('Title', validators=[DataRequired()])
    content = TextAreaField('Content', validators=[DataRequired()])
    submit = SubmitField('Post')

Tags: textimagevaluetypelinkscriptcontentjavascript
2条回答

我花了相当多的时间来解决类似的问题,我也参考了该网站。我需要添加一些额外的代码,以使其适用于我的flask应用程序。我会在这里张贴,也许有人能找到它的帮助

首先,我需要在tinymce.init中添加以下代码:

        setup: function (editor) {
    editor.on('change', function (e) {
        editor.save();
    });

这允许我发布编辑过的内容,但渲染仍然是个问题。为了解决这个问题,我需要在我的jinja模板中添加| safe,如下所示:

    {% for post in posts.items %}
<article class="media content-section">
  <img class="rounded-circle article-img" src="{{url_for('static', filename='images/' + post.author.image_file) }}" alt="">
  <div class="media-body">
    <div class="article-metadata">
      <a class="mr-2" href="{{url_for('user_posts', username=post.author.username)}}">{{ post.author.username }}</a>
      <small class="text-muted">{{ post.date_posted.strftime('%Y-%m-%d') }}</small>
    </div>
    <h2><a class="article-title" href="{{url_for('post', post_id=post.id)}}">{{ post.title }}</a></h2>
    <p class="article-content">{{ post.content|safe }}</p>
  </div>
</article>
{% endfor %}

你看,在我的内容后面有“安全”。我知道我可以用tinymce渲染我编辑的所有内容

这是那篇文章的作者,顺便说一句,这篇文章的url改成了

https://www.kevin7.net/post_detail/tinymce-and-flask

我注意到你的代码有一个输入错误

你有:

script type="textarea/javascript"

但应该是这样

script type="text/javascript"

除此之外,一切看起来都很好。我把你的代码复制到我的电脑上,它可以正常工作

我还假设您正在将表单传递给模板?我在最初的博客帖子中遗漏了这一点,我将尝试更新博客,以尽快包含所有细节

相关问题 更多 >