How to get data from TinyMCE to flask view

Viewed 779

I have a text editor form - TinyMCE, but cannot make it pass the text to flask view function. I used this tutorial to add the text editor https://www.tiny.cloud/blog/bootstrap-wysiwyg-editor/. TinyMCE uses bootstrap. My app.py file:

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()

My index.html file:

{% 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 %}

I have added form action but it did not help. The error I am getting is TypeError: Can't convert 'NoneType' object to str implicitly So form is not parsed by the view function.

1 Answers

You're Posting the data in a form not the request parameters. You need to look in flask.request.form

HTML

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

Flask

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>'
Related