Browse Source

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

master
Cristina Cochior 5 years ago
parent
commit
1744735471
  1. 26
      startform.py
  2. 20
      templates/description-prev.html

26
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)
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"

20
templates/description-prev.html

@ -0,0 +1,20 @@
{% block content %}
<div class="description">
<h2 style = "text-align: center;">Write something very interesting here.</h2>
{% for message in form.name.errors %}
<div>{{ message }}</div>
{% endfor %}
<form method="POST" action="/get-data">
<fieldset>
{{ form.hidden_tag() }}
<div>{{ form.name.label }} {{ form.name }}</div>
<div>{{ form.email.label }} {{ form.email }}</div>
<div>{{ form.friend.label }} {{ form.friend }}</div>
<div>{{ form.content.label }} {{ form.content }}</div>
<div>{{ form.submit }}</div>
</fieldset>
</form>
</div>
{% endblock %}
Loading…
Cancel
Save