Creating a guestbook application is a classic web development project, and Flask is a great choice for building one. In this guide, we'll explore how to create a simple guestbook with Flask, allowing visitors to leave comments and messages. By following this guide, you'll have a foundation for building more advanced guestbook applications with Flask.

Step 1: Setting Up Your Flask Application

Start by setting up your Flask application and installing the necessary extensions. Here's a sample directory structure:


Step 2: Creating a Guestbook Model

Create a model to store guestbook entries in a database. Here's an example of how to define a GuestEntry model using Flask-SQLAlchemy:

from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///guestbook.db'
db = SQLAlchemy(app)
class GuestEntry(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
message = db.Column(db.Text, nullable=False)
def index():
entries = GuestEntry.query.all()
return render_template('index.html', entries=entries)
if __name__ == '__main__':

Step 3: Creating Guestbook Templates

Create HTML templates for the guestbook application. Here's a basic structure for your index template (index.html) for displaying guestbook entries:

<!DOCTYPE html>
{% for entry in entries %}
<strong>{{ }}</strong> says:
<p>{{ entry.message }}</p>
{% endfor %}

And here's a basic structure for your guestbook template (guestbook.html) for submitting new guestbook entries:

<!DOCTYPE html>
<title>Add Entry</title>
<h1>Add Entry</h1>
<h2>Leave a Message</h2>
<form method="post" action="/add_entry">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name" required>
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" required></textarea>
<button type="submit">Submit</button>

Step 4: Routing for Guestbook

Create routes for viewing and adding guestbook entries. Here's an example of how to define these routes:

def guestbook():
return render_template('guestbook.html')
@app.route('/add_entry', methods=['POST'])
def add_entry():
name = request.form['name']
message = request.form['message']
entry = GuestEntry(name=name, message=message)
return redirect('/')

Step 5: Running Your Guestbook

Run your Flask guestbook application using the following command:


Access your web application in a browser, and visitors can leave guestbook entries.


Creating a guestbook with Flask is a simple and enjoyable web development project. By following the steps in this guide, you can set up your Flask application, create templates for displaying and submitting guestbook entries, and enable visitors to leave messages. Enhance your guestbook application with features like date/time stamps and user authentication.