We need 'datetime' module for getting the current time
from datetime import datetime
We need the 're' module to validate email address using regular expressions
import re
We import 'Flask' to create an app,
'request' to access the request data in each view
'flash' to send messages to be displayed in the template
'url_for' to get the URL for a given view function name
'redirect' to redirect to a given URL
'render_template' to render a template
from flask import Flask, request, flash, url_for, redirect, render_template
'flask_sqlalchemy' provides a wrapper over SQLAlchemy. Install
'flask-sqlalchemy' package to use this.
from flask_sqlalchemy import SQLAlchemy
app = Flask(name)
Reads the settings from a configuration file
app.config.from_pyfile('app.cfg')
db = SQLAlchemy(app)
We are defining a 'Comments' model to store the comments the user
enters via the form.
class Comments(db.Model):
tablename = 'comments' # Setting the table name
Creating columns for various fields
id = db.Column('comment_id', db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
comment = db.Column(db.String(200))
pub_date = db.Column(db.DateTime)
def init(self, name, email, comment):
# Initializes the fields with entered data
self.name = name
self.email = email
self.comment = comment
# Initializes the published date to the current time
self.pub_date = datetime.now()
def is_email_address_valid(email):
"""Validate email address using regular expression."""
if not re.match("^[a-zA-Z0-9.!#", email):
return False
return True
The default route for the app.
Displays the list of already entered comments
@app.route('/')
def show_all():
# We are getting all the comments ordered in
# descending order of pub_date and passing to the
# template via 'comments' variable
return render_template('show_all.html',
comments=Comments.query.order_by(Comments.pub_date.desc()).all()
)
This view method responds to the URL /new for the methods GET and POST
@app.route('/new', methods=['GET', 'POST'])
def new():
if request.method == 'POST':
# The request is POST with some data, get POST data and validate it.
# The form data is available in request.form dictionary.
# Check if all the fields are entered. If not, raise an error
if not request.form['name'] or not request.form['email'] or not request.form['comment']:
flash('Please enter all the fields', 'error')
# Check if the email address is valid. If not, raise an error
elif not is_email_address_valid(request.form['email']):
flash('Please enter a valid email address', 'error')
else:
# The data is valid. So create a new 'Comments' object
# to save to the database
comment = Comments(request.form['name'],
request.form['email'],
request.form['comment'])
# Add it to the SQLAlchemy session and commit it to
# save it to the database
db.session.add(comment)
db.session.commit()
# Flash a success message
flash('Comment was successfully submitted')
# Redirect to the view showing all the comments
return redirect(url_for('show_all'))
# Render the form template if the request is a GET request or
# the form validation failed
return render_template('new.html')