Introduction

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:

guestbook-app/
app.py
templates/
index.html
guestbook.html

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:

# app.py
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)
@app.route('/')
def index():
entries = GuestEntry.query.all()
return render_template('index.html', entries=entries)
if __name__ == '__main__':
app.run(debug=True)

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>
<html>
<head>
<title>Guestbook</title>
</head>
<body>
<header>
<h1>Guestbook</h1>
</header>
<section>
<h2>Entries</h2>
<ul>
{% for entry in entries %}
<li>
<strong>{{ entry.name }}</strong> says:
<p>{{ entry.message }}</p>
</li>
{% endfor %}
</ul>
</section>
</body>
</html>

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

<!DOCTYPE html>
<html>
<head>
<title>Add Entry</title>
</head>
<body>
<header>
<h1>Add Entry</h1>
</header>
<section>
<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>
</form>
</section>
</body>
</html>

Step 4: Routing for Guestbook

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

# app.py
@app.route('/guestbook')
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)
db.session.add(entry)
db.session.commit()
return redirect('/')

Step 5: Running Your Guestbook

Run your Flask guestbook application using the following command:

python app.py

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


Conclusion

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.