Prerequisites
Make sure you have Python and Flask installed. You can install Flask using pip:
pip install Flask
Project Structure
volunteer_management/
│
├── app.py
├── templates/
│ ├── index.html
│ ├── add_volunteer.html
│ ├── edit_volunteer.html
└── database.db
Step 1: Setting Up the Database
Create a SQLite database and a table for volunteers.
# db_setup.py
import sqlite3
def setup_database():
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS volunteers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
phone TEXT NOT NULL
)
''')
conn.commit()
conn.close()
if __name__ == '__main__':
setup_database()
Run this script once to create the database:
python db_setup.py
Step 2: Create the Flask Application
Create the main application file app.py
:
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
# Function to get database connection
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
# Route to display all volunteers
@app.route('/')
def index():
conn = get_db_connection()
volunteers = conn.execute('SELECT * FROM volunteers').fetchall()
conn.close()
return render_template('index.html', volunteers=volunteers)
# Route to add a new volunteer
@app.route('/add', methods=('GET', 'POST'))
def add_volunteer():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
conn = get_db_connection()
conn.execute('INSERT INTO volunteers (name, email, phone) VALUES (?, ?, ?)',
(name, email, phone))
conn.commit()
conn.close()
return redirect(url_for('index'))
return render_template('add_volunteer.html')
# Route to edit a volunteer
@app.route('/edit/<int:id>', methods=('GET', 'POST'))
def edit_volunteer(id):
conn = get_db_connection()
volunteer = conn.execute('SELECT * FROM volunteers WHERE id = ?', (id,)).fetchone()
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
conn.execute('UPDATE volunteers SET name = ?, email = ?, phone = ? WHERE id = ?',
(name, email, phone, id))
conn.commit()
conn.close()
return redirect(url_for('index'))
conn.close()
return render_template('edit_volunteer.html', volunteer=volunteer)
# Route to delete a volunteer
@app.route('/delete/<int:id>', methods=('POST',))
def delete_volunteer(id):
conn = get_db_connection()
conn.execute('DELETE FROM volunteers WHERE id = ?', (id,))
conn.commit()
conn.close()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
Step 3: Create HTML Templates
Create the HTML templates in the templates/
directory.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Volunteer Management</title>
</head>
<body>
<h1>Volunteers</h1>
<a href="/add">Add Volunteer</a>
<ul>
{% for volunteer in volunteers %}
<li>
{{ volunteer.name }} - {{ volunteer.email }} - {{ volunteer.phone }}
<a href="/edit/{{ volunteer.id }}">Edit</a>
<form action="/delete/{{ volunteer.id }}" method="post" style="display:inline;">
<button type="submit">Delete</button>
</form>
</li>
{% endfor %}
</ul>
</body>
</html>
add_volunteer.html
<!DOCTYPE html>
<html>
<head>
<title>Add Volunteer</title>
</head>
<body>
<h1>Add Volunteer</h1>
<form method="post">
<label>Name:</label>
<input type="text" name="name" required>
<br>
<label>Email:</label>
<input type="email" name="email" required>
<br>
<label>Phone:</label>
<input type="text" name="phone" required>
<br>
<button type="submit">Add Volunteer</button>
</form>
</body>
</html>
edit_volunteer.html
<!DOCTYPE html>
<html>
<head>
<title>Edit Volunteer</title>
</head>
<body>
<h1>Edit Volunteer</h1>
<form method="post">
<label>Name:</label>
<input type="text" name="name" value="{{ volunteer.name }}" required>
<br>
<label>Email:</label>
<input type="email" name="email" value="{{ volunteer.email }}" required>
<br>
<label>Phone:</label>
<input type="text" name="phone" value="{{ volunteer.phone }}" required>
<br>
<button type="submit">Update Volunteer</button>
</form>
</body>
</html>
Step 4: Run the Application
You can run the application using:
python app.py
Access the application in your web browser at http://127.0.0.1:5000/
.
Leave a Reply