Python Forum

Full Version: how i save the html form to flask database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need help.
I want to this input area save the flask database.
how i save the html form to flask?

https://hizliresim.com/grrMm5

https://hizliresim.com/P11JX5
(Feb-06-2019, 08:50 PM)mebaysan Wrote: [ -> ]how i save the html form to flask?
The page you have is messy,i guess it's a tempalte you use.
Nothing wrong with that,but when learn this is has a lot stuff that is confusing,as this form is setup to use ajax-request.
Do you know how a basic form work in HTML?

To take out the form out of site and make it work Pen.
You see that it work and i have put in action="#" method="POST".
# is just so that it work on CodePen.
action name is what you call function on server side Flask.
name="comment_content"(<input>) get send when push Submit.

To show this setup in Flask.
The stander and best way for DB in Flask is to use Flask-SQLAlchemy.
app.py:
from flask import Flask, render_template, jsonify, request
 
app = Flask(__name__)
@app.route('/')
def index():
   return render_template("index.html")
 
@app.route('/my_form', methods=['POST'])
def my_form():
    form_input = request.form['comment_content']
    # Now that get value back to server can send it to a DB(use Flask-SQLAlchemy)
    return(form_input)

if __name__ == '__main__':
   app.run(debug=True)
index.html:
<!doctype html>
<html>
  <head>
    <title>Some title</title>
    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
  </head>
  <body>
    <div id="start">
      <h1>Start page</h1>

      <form action="{{ url_for('my_form') }}" method='POST'>
        <div>
          <textarea id="comment_content" name="comment_content"></textarea>
          <span class="character-counter" ></span></div>
        <div>
          <button type="submit" class="btn waves-effect waves-light">G├ľNDER</button>
        </div>
      </form>

    </div>
  </body>
</html>