Split out name to first and last, only displaying First name field and location field.

This commit is contained in:
Steve Dogiakos 2025-04-01 17:40:08 -06:00
parent 35563a4d08
commit e18fd5fffa
2 changed files with 17 additions and 10 deletions

18
app.py
View File

@ -10,7 +10,8 @@ def init_db():
c.execute('''
CREATE TABLE IF NOT EXISTS guests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL,
location TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
@ -22,21 +23,24 @@ def init_db():
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
name = request.form.get('name')
first_name = request.form.get('first_name')
last_name = request.form.get('last_name')
email = request.form.get('email')
location = request.form.get('location')
if name and email and location:
if first_name and last_name and email and location:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('INSERT INTO guests (name, email, location) VALUES (?, ?, ?)',
(name, email, location))
c.execute(
'INSERT INTO guests (first_name, last_name, email, location) VALUES (?, ?, ?, ?)',
(first_name, last_name, email, location)
)
conn.commit()
conn.close()
return redirect(url_for('index'))
# Retrieve guest entries to display on the page.
# Retrieve guest entries to display only first name and location.
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('SELECT name, email, location, timestamp FROM guests ORDER BY id DESC')
c.execute('SELECT first_name, location FROM guests ORDER BY id DESC')
guests = c.fetchall()
conn.close()
return render_template('index.html', guests=guests)

View File

@ -9,8 +9,11 @@
<body>
<h1>Museum Visitor Guestbook</h1>
<form method="post" action="/">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>
<label for="first_name">First Name(s):</label><br>
<input type="text" id="first_name" name="first_name" required><br><br>
<label for="last_name">Last Name:</label><br>
<input type="text" id="last_name" name="last_name" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
@ -24,7 +27,7 @@
<h2>Guest Entries</h2>
<ul>
{% for guest in guests %}
<li><strong>{{ guest[0] }}</strong> from {{ guest[2] }} (<em>{{ guest[1] }}</em>) at {{ guest[3] }}</li>
<li><strong>{{ guest[0] }}</strong> from {{ guest[1] }}</li>
{% endfor %}
</ul>
</body>