Skip to content

Storing Form Data

Everything is connected. The form collects data, Flask receives it, PookieDB saves it to the database, and the page shows the updated list — all of it surviving restarts.


The complete project

Project structure:

flask-project/
├── .venv/
├── templates/
│   └── index.html
├── database.py
├── app.py
└── patients.sqlite3   ← appears automatically on first run

database.py

import pookiedb

pookiedb.connect("sqlite:///patients.sqlite3")

class Patient(pookiedb.Model):
    name = pookiedb.CharField(max_length=100)
    age  = pookiedb.IntegerField()

    class Meta:
        db_table = "patients"

Patient.create_table()

app.py

from flask import Flask, render_template, request, redirect, url_for
from database import Patient

app = Flask(__name__)

@app.route('/')
def home():
    patients = Patient.objects.all()
    return render_template('index.html', patients=patients)

@app.route('/add', methods=['POST'])
def add():
    Patient.objects.create(
        name=request.form["patient_name"],
        age=int(request.form["age"])
    )
    return redirect(url_for('home'))

if __name__ == '__main__':
    app.run(debug=True)

templates/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Patients</title>
  </head>
  <body>

    <h1>Add Patient</h1>

    <form action="/add" method="post">
        <label for="patient_name">Patient Name</label>
        <input type="text" id="patient_name" name="patient_name" placeholder="Full name">

        <label for="age">Age</label>
        <input type="number" id="age" name="age" placeholder="Age">

        <button type="submit">Add Patient</button>
    </form>

    <h2>Patients</h2>

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
        </tr>
      </thead>
      <tbody>
        {% for patient in patients %}
        <tr>
          <td>{{ patient.name }}</td>
          <td>{{ patient.age }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>

  </body>
</html>

The template does not change at all from the in-memory version. Jinja2's {{ patient.name }} works on PookieDB model objects exactly the same as it did on plain dictionaries.


How to know it is working

  1. Run python app.py
  2. Visit http://127.0.0.1:5000
  3. The form appears, the table is empty
  4. Add a patient — they appear in the table
  5. Add two more — all three appear
  6. Stop the app with Ctrl + C
  7. Run python app.py again and visit the page — all three patients are still there

Step 7 is the difference from before. The data survived the restart because it is in the database file, not in memory.


The only things that changed from the in-memory version

  • patients = [] is gone
  • patients.append(person) is replaced by Patient.objects.create(...)
  • render_template('index.html', patients=patients) now passes Patient.objects.all() instead of the list
  • Everything else — the routes, the template, the form — is identical

← Connecting PookieDB to Flask    Next: What is Bootstrap? →