Programming lesson
Building a Baking Contest User Manager with Flask and SQLite3
Learn how to build a basic Flask web application with SQLite3 database integration by creating a Baking Contest User Manager. This tutorial covers input validation, database CRUD operations, and dynamic HTML templates.
Introduction: Why Flask + SQLite3 for Small-Scale Web Apps?
Flask is a lightweight Python web framework that makes it easy to build web applications quickly. Combined with SQLite3 (a serverless database engine), you can create functional data-driven websites without heavy infrastructure. In this tutorial, we'll build a Baking Contest User Manager—a simple CRUD app that lets you add, list, and manage baking contest participants. This mirrors real-world scenarios like event registration systems, club member databases, or even AI-powered recipe sharing platforms that are trending in 2026.
Prerequisites
- Python 3.6+ installed
- Basic knowledge of Python and HTML
- Familiarity with the command line
Step 1: Setting Up Your Flask Project
Create a new directory for your project and set up a virtual environment (optional but recommended). Then install Flask:
pip install flaskCreate a file named app.py. This will be the main entry point for your Flask application.
Step 2: Creating the SQLite3 Database Schema
We need two tables: baking_contest_people (for users) and baking_contest_entries (for contest results). Use the following SQL to create them:
CREATE TABLE baking_contest_people (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL,
phone_number TEXT NOT NULL,
security_level INTEGER NOT NULL,
login_password TEXT NOT NULL
);
CREATE TABLE baking_contest_entries (
entry_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
name_of_baking_item TEXT,
num_excellent_votes INTEGER,
num_ok_votes INTEGER,
num_bad_votes INTEGER,
FOREIGN KEY (user_id) REFERENCES baking_contest_people(id)
);Insert some sample data for testing (e.g., a few entries with votes).
Step 3: Building the Flask Routes
Home Page
The home page displays links to other pages. Create a route / that renders home.html.
@app.route('/')
def home():
return render_template('home.html')In home.html, include three links: Add New Baking Contest User, List Baking Contest Users, and Baking Contest Results.
Add User Page
Create a route /add_user that handles both GET and POST requests. On GET, render the form. On POST, validate inputs and insert into the database.
Validation rules:
- Name: not empty and not only spaces
- Age: whole number > 0 and < 121
- Phone Number: not empty and not only spaces
- Security Level: numeric between 1 and 3
- Login Password: not empty and not only spaces
If validation fails, collect error messages and pass them to the results page. If successful, insert the record and set a success message.
List Users Page
Route /list_users queries all records from baking_contest_people and displays them in an HTML table.
List Contest Results Page
Route /list_results queries all records from baking_contest_entries and displays them.
Results Page
Route /results displays a message (either success or error) passed via query string or session.
Step 4: Creating HTML Templates
Use Flask's Jinja2 templating engine. Create a templates folder and add the following files:
home.html– with linksadd_user.html– form with input fieldslist_users.html– table of userslist_results.html– table of entriesresults.html– displays the message
Example snippet for add_user.html:
<h2>Add a New Baking Contest User</h2>
<form method="POST">
<label>Name:</label>
<input type="text" name="name"><br>
<label>Age:</label>
<input type="number" name="age"><br>
<label>Phone Number:</label>
<input type="text" name="phone_number"><br>
<label>Security Level (1-3):</label>
<input type="number" name="security_level"><br>
<label>Login Password:</label>
<input type="password" name="login_password"><br>
<input type="submit" value="Submit">
</form>Step 5: Input Validation Logic
In the /add_user POST handler, validate each field. Example:
errors = []
name = request.form.get('name', '').strip()
if not name:
errors.append('Name is required.')
age = request.form.get('age', '')
try:
age = int(age)
if age <= 0 or age >= 121:
errors.append('Age must be between 1 and 120.')
except ValueError:
errors.append('Age must be a whole number.')
# ... similar for other fieldsIf errors exist, redirect to /results?msg=' + '; '.join(errors). Otherwise, insert and redirect with success message.
Step 6: Running the Application
To run your Flask app, use:
python app.pyVisit http://127.0.0.1:5000/ in your browser. Test all pages and validation.
Step 7: Testing and Debugging
Make sure to test edge cases:
- Empty name
- Age = 0 or 121
- Security level = 0 or 4
- Spaces-only password
Check that the database updates correctly. Use SQLite browser or command line to verify records.
Conclusion: From Baking Contest to Real-World Apps
You've built a fully functional Flask + SQLite3 web application! This pattern is used in countless real-world projects: event registration systems, inventory management, social media dashboards, and even AI training data collection tools (a hot trend in 2026). By mastering these basics, you're ready to tackle more complex projects like integrating REST APIs, user authentication, or deploying to the cloud.
Keep experimenting—try adding a search feature, sorting, or even a simple login system. Happy coding!