From 1744735471d2a1dd7d0f773ce92526724be45ead Mon Sep 17 00:00:00 2001 From: Cristina Cochior Date: Fri, 11 Jan 2019 03:25:53 +0100 Subject: [PATCH] the form now saves output to a json file. readded the old version of the description template under the name description-prev.html to remove call to layout.html --- startform.py | 26 ++++++++++++++++---------- templates/description-prev.html | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 templates/description-prev.html diff --git a/startform.py b/startform.py index bda0b2d..2c5eb5c 100644 --- a/startform.py +++ b/startform.py @@ -3,6 +3,7 @@ from flask import Flask, url_for, render_template, Markup, redirect, request, fl from flask import session as login_session from forms import ReusableForm from config import Config +import json_actions import json app = Flask(__name__, static_url_path='', static_folder="static", template_folder="templates") @@ -13,16 +14,21 @@ app.config.from_object(Config) def description(): form = ReusableForm(request.form) print (form.errors) - if request.method == 'POST' and form.validate(): - return 'Success' - return render_template('description.html', form=form) - + if request.method == 'POST' and form.validate(): + return 'Success!' + return render_template('description-prev.html', form=form) if __name__ == '__main__': app.run(debug = True) - - - - - - \ No newline at end of file +dict={} + +@app.route('/get-data', methods=['GET', 'POST']) +def savepost(): + if request.method=='POST': + dict={'name':request.form['name'],'email':request.form['email'],'friend':request.form['friend'],'content':request.form['content']} + # open with "a" if the file should concatenate content + with open("data_file.json", "w") as write_file: + json.dump(dict, write_file) + return "The JSON file is ready." + else: + return "Error" \ No newline at end of file diff --git a/templates/description-prev.html b/templates/description-prev.html new file mode 100644 index 0000000..5369d22 --- /dev/null +++ b/templates/description-prev.html @@ -0,0 +1,20 @@ +{% block content %} +
+

Write something very interesting here.

+ {% for message in form.name.errors %} +
{{ message }}
+ {% endfor %} + +
+
+ {{ form.hidden_tag() }} +
{{ form.name.label }} {{ form.name }}
+
{{ form.email.label }} {{ form.email }}
+
{{ form.friend.label }} {{ form.friend }}
+
{{ form.content.label }} {{ form.content }}
+
{{ form.submit }}
+
+
+
+ {% endblock %} +