1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| from random import randint
from time import strftime
from flask import Flask, render_template, flash, request
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = 'SjdnUends821Jsdlkvxh391ksdODnejdDw'
class ReusableForm(Form):
name = TextField('Name:', validators=[validators.required()])
surname = TextField('Surname:', validators=[validators.required()])
def get_time():
time = strftime("%Y-%m-%dT%H:%M")
return time
def write_to_disk(name, surname, email):
data = open('file.log', 'a')
timestamp = get_time()
data.write('DateStamp={}, Name={}, Surname={}, Email={} \n'.format(timestamp, name, surname, email))
data.close()
@app.route("/", methods=['GET', 'POST'])
def hello():
form = ReusableForm(request.form)
#print(form.errors)
if request.method == 'POST':
name=request.form['name']
surname=request.form['surname']
email=request.form['email']
password=request.form['password']
if form.validate():
write_to_disk(name, surname, email)
flash('Hello: {} {}'.format(name, surname))
else:
flash('Error: All Fields are Required')
return render_template('index.html', form=form)
if __name__ == "__main__":
app.run()
|