0

I'm trying to get the data from this html form, through flask and store it in python variables so that I can access and process later. In the python code I have req = request.formand that returns a dictionary which contains all the info I need but I can't acces it outside the form_input() function. Is there anyway I can do this ? Thanks

python code

from flask import Flask, request, redirect, render_template

app = Flask(__name__)

path = r'C:\Users\tiberiu.ghimbas\PycharmProjects\pythonProject\form.html'
lista = []

app.testing = True
client = app.test_client()

@app.route('/')
def home():
    return render_template('form.html')


@app.route('/submit_form', methods=['GET', 'POST'])
def form_input():

    if request.method == "POST":
        req = request.form
        last_name = request.form['lname']
        first_name = request.form['fname']
        email = request.form['email']
        department = request.form['departament']
        data_angajare = request.form['data_angajare']

        return redirect(request.url)

    return render_template('form.html')


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

html

<html>
  <head>
    <title>Form</title>
  </head>
  <body>
    <form action = "/submit_form" method = "post">
      <label for = "fname">First name:</label>
      <input type = "text" id = "fname" name = "fname"><br>

      <label for = "lname">Last name:</label>
      <input type = "text" id = "lname" name = "lname"><br><br>

      <label for = "mail">Email:</label>
      <input type = "text" id = "mail" name = "email"></input> <br><br>

      <input type = "radio" id = "1" name = "departament" value = "Hotel">
      <label for = "1">Hotel</label>

      <input type = "radio" id = "2" name = "departament" value = "Vanzari">
      <label for = "2">Vanzari</label>

      <input type = "radio" id = "3" name = "departament" value = "Marketing">
      <label for = "3">Marketing</label>

      <input type = "radio" id = "4" name = "departament" value = "IT">
      <label for = "4">IT</label>
      <br>
      <br>

      <label for = "date">Data angajarii: </label>
      <input type = "date" id = "date" name = "data_angajare"> <br><br>

      <input type = "submit" value = "submit"></input>

    </form>

  </body>
</html>
iss_628
  • 83
  • 5
  • 1
    Why you don't store req into some global collection? Usually you are using flask sessions for this. – iss_628 Jun 30 '21 at 07:58

1 Answers1

1

You have multiple ways of achieving this:

  1. Store the data in a global variable in either your current or another python script. This is the quickest way of doing so, but the data will be lost upon restarting the server. You will also run into issues if you later put your flask behind something that uses workers, as they will have different values inside your global variable. There is also the flask g object. It's a global variable that is only valid during one request, but you can access its data from anywhere.

  2. Save the info in one (or multiple) files. It's a little more work, but the data is persistent, and there will be no issues with workers.

  3. Use a database (recommended). Flask comes with sqlite, but I personally prefer pymysql (https://pypi.org/project/PyMySQL/) for databases. You will gain the benefits from option 2, but you will also be able to search through your data easily & efficiently, which may or may not be desired. Most web applications use databases to store their data.

  4. Use the flask session object. You can save any user/session relevant data inside this object, which will be unique per session. The data is saved in a cookie in the user's browser (encrypted). It's not 100% safe, so don't put any passwords or anything similar in there. By default a flask session lasts until the user closes the browser (browser-dependent), but you can configure flask to save it longer. The data will carry over from request to request, as long as it's the same user and the session cookie exists.

Max Leidl
  • 56
  • 3