From 3d2e637d57fffc5f3bb99ba8c178d3ec2cca742c Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 15 Mar 2022 18:40:08 -0600 Subject: [PATCH] Save form submissions --- .gitignore | 1 + main.py | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 485dee6..d988c38 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +dll_data diff --git a/main.py b/main.py index 2a74573..c71c930 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,43 @@ +import os, json + from flask import Flask, flash, redirect, render_template, request, session, abort app = Flask(__name__) +PROJECT_PATH = os.path.dirname(__file__) +DATA_DIR = os.path.join(PROJECT_PATH, "dll_data") + +if not os.path.exists(DATA_DIR): + os.mkdir(DATA_DIR) + + @app.route("/") def index(): return "Index!" + @app.route("/hello") def hello(): return "Hello World!" + @app.route("/form") def form(): return render_template( 'form.html',**locals()) + @app.route('/form_post', methods = ['POST']) def form_post(): if request.method == 'POST': data = request.form print("data: {}".format(data)) - # TODO: save this data to a csv - # deploy this python app to server somewhere - # with a particular domain + filename = data['fname'] + "-" + data['lname'] + file_path = os.path.join(DATA_DIR, filename) + with open(file_path, 'w') as f: + f.write(json.dumps(data)) return "form submitted" + if __name__ == "__main__": app.run() \ No newline at end of file