You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

  1. from flask import Flask, render_template, session, redirect, url_for, flash
  2. from flask_bootstrap import Bootstrap
  3. from flask_moment import Moment
  4. from flask_wtf import FlaskForm
  5. from wtforms import StringField, SubmitField
  6. from wtforms.validators import DataRequired
  7. app = Flask(__name__)
  8. app.config['SECRET_KEY'] = 'hard to guess string'
  9. bootstrap = Bootstrap(app)
  10. moment = Moment(app)
  11. class NameForm(FlaskForm):
  12. name = StringField('What is your name?', validators=[DataRequired()])
  13. submit = SubmitField('Submit')
  14. @app.errorhandler(404)
  15. def page_not_found(e):
  16. return render_template('404.html'), 404
  17. @app.errorhandler(500)
  18. def internal_server_error(e):
  19. return render_template('500.html'), 500
  20. @app.route('/', methods=['GET', 'POST'])
  21. def index():
  22. form = NameForm()
  23. if form.validate_on_submit():
  24. old_name = session.get('name')
  25. if old_name is not None and old_name != form.name.data:
  26. flash('Loks like you have changed your name!')
  27. session['name'] = form.name.data
  28. return redirect(url_for('index'))
  29. return render_template('index.html', form=form, name=session.get('name'))