1
The purpose of this file is to create the application interface, establish routes to other html documents and to launch the server that runs the application itself. An example of this file is shown below. This example will include three pages (home, services and contact)
main Python file
copy and paste the following code into the main.py document created. Ensure the lines that require indentation are all the same amount of spaces. Four spaces is ideal.
#!/usr/bin/env python # -*- coding: utf-8 -*- # import modules # ------------------ from __future__ import unicode_literals import os from flask import Flask import cgitb from flask import render_template from forms import MessageForm from waitress import serve # create application # --------------------- # enable cgitb: cgitb.enable() # create app: app = Flask(__name__) # create interface: wsgi_app = app.wsgi_app # create key: app.config['SECRET_KEY'] = 'random_key_here' # establish routes (home, services, contact, form_passed, form_error) # -------------------------------------------------------------------------------- @app.route('/') @app.route('/index') def index(): return render_template('index.html', title='Home') @app.route('/services') def services(): return render_template('services.html', title='Services') @app.route('/contact', methods=['GET', 'POST']) def contact(): form = MessageForm() return render_template('contact.html', title='Contact', form=form) @app.route('/contact/form_passed') def form_passed(): return render_template('form_passed.html', title='Form Passed') @app.route('/contact/form_error') def form_error(): return render_template('form_error.html', title='Form Error') # start application (development) # ------------------------------------- # debugging if __name__ == '__main__': HOST = os.environ.get('SERVER_HOST', 'localhost') try: PORT = int(os.environ.get('SERVER_PORT', '5000')) except ValueError: PORT = 5000 app.debug = True app.run(host='0.0.0.0') # start application (production) # ----------------------------------- # if __name__ == "__main__": # serve(app, host="0.0.0.0", port=5000, threads=8)
This document is created to validate form data sent from the contact form. If the amount of characters entered into the form field is under 2, the form will fail. If too much data is submitted, the form will also fail. Upon doing so, the user will be prompted to correct the error and try again.
Copy and paste the following into forms.py
Ensure all indentation is the same (4 spaces)
# -*- coding: utf-8 -*- from __future__ import unicode_literals from flask_wtf import FlaskForm from wtforms import StringField, SubmitField from wtforms.validators import DataRequired, Length, Email class MessageForm(FlaskForm): name = StringField(validators=[DataRequired(), Length(min=2, max=20)]) mail = StringField(validators=[DataRequired(), Email()]) subject = StringField(validators=[DataRequired(), Length(min=2, max=50)]) message = StringField(validators=[DataRequired(), Length(min=2, max=500)]) submit = SubmitField('Submit')