-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
56 lines (42 loc) · 1.8 KB
/
hello.py
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
47
48
49
50
51
from flask import Flask, render_template, session, redirect, url_for , flash
from flask import request
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from datetime import datetime
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import Email, DataRequired, ValidationError
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
bootstrap = Bootstrap(app)
moment = Moment(app)
def validate_email(form, field):
if "@" not in field.data:
raise ValidationError(f"Please include '@' in the email address. {field.data} is missing an '@'")
class NameForm(FlaskForm):
name = StringField('What is your name?', validators=[DataRequired()])
email = StringField('What is your UofT email adderess?', validators=[validate_email])
submit = SubmitField('Submit')
@app.route('/user/<name>')
def user(name):
return render_template('index.html', name=name)
@app.route('/', methods=['GET', 'POST'])
def index():
form = NameForm()
if form.validate_on_submit():
name = form.name.data
email = form.email.data
old_name = session.get('name')
old_email = session.get('email')
if old_name is not None and old_name != form.name.data:
flash('Looks like you have changed your name!')
if old_email is not None and old_email != form.email.data:
flash('Looks like you have changed your email!')
session['name'] = form.name.data
session['email'] = form.email.data
return redirect(url_for('index'))
return render_template('index.html', form=form, name=session.get('name') , email=session.get('email'))
@app.errorhandler(404)
def page_notfound(e):
return render_template('404.html'), 404