如何从TinyMCE获得数据到flask视图

问题描述 投票:0回答:1

我有一个文本编辑窗体--TinyMCE,但不能让它把文本传递到flask视图功能。我用这个教程添加了文本编辑器。https:/www.tiny.cloudblogbootstrap-wysiwyg-editor. TinyMCE使用bootstrap。我的app.py文件。

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/')
def index():
    editor = request.args.get("editor")
    print(editor)
    return render_template('index.html')

@app.route('/see_posts', methods=['POST'])
def see_posts():
    editor = request.args.get('editor')
    return "<p>"+editor+'</p>'


if __name__ == '__main__':
    app.run()

我的index.html文件:

{% extends "base.html" %}

{% block content %}
    <script src="https://cdn.tiny.cloud/1/r55dmb7tylbap7uwxto0jlsvcn6z3uy29kieq6ujtxejkzyi/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>

<script>
  tinymce.init({
    selector: 'textarea#editor',
    menubar: false
  });
</script>
<div class="container mt-4 mb-4">
  <div class="row justify-content-md-center">
    <div class="col-md-12 col-lg-8">
     <form action = {{ url_for('see_posts') }} method='POST'}}>
      <h1 class="h2 mb-4">Submit issue</h1>
      <label>Describe the issue in detail</label>
      <div class="form-group">

         <textarea id="editor" name = editor></textarea>
      </div>
      <button type="submit" class="btn btn-primary">Submit</button>
     </form>
    </div>
  </div>
</div>
{% endblock %}

我已经添加了表单动作,但没有任何帮助。我得到的错误是 TypeError: Can't convert 'NoneType' object to str implicitly 所以表单不被视图函数解析。

python twitter-bootstrap flask tinymce
1个回答
2
投票

你在表单中发布数据,而不是请求参数。 你需要看在 flask.request.form

超文本标记语言

<form action="{{ url_for('see_posts') }}" method='POST'}}>
    <textarea id="editor" name="editor"></textarea>
    <button type="submit">Submit</button>
</form>

烧瓶

from flask import request, render_template
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/see_posts', methods=['POST'])
def see_posts():
    editor = request.form['editor']
    return  "<p>"+editor+'</p>'
© www.soinside.com 2019 - 2024. All rights reserved.