Volunteer Management System

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&#91;'name']
    email = request.form&#91;'email']
    phone = request.form&#91;'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>
&lt;title>Volunteer Management&lt;/title>
</head> <body>
&lt;h1>Volunteers&lt;/h1>
&lt;a href="/add">Add Volunteer&lt;/a>
&lt;ul>
    {% for volunteer in volunteers %}
        &lt;li>
            {{ volunteer.name }} - {{ volunteer.email }} - {{ volunteer.phone }}
            &lt;a href="/edit/{{ volunteer.id }}">Edit&lt;/a>
            &lt;form action="/delete/{{ volunteer.id }}" method="post" style="display:inline;">
                &lt;button type="submit">Delete&lt;/button>
            &lt;/form>
        &lt;/li>
    {% endfor %}
&lt;/ul>
</body> </html>

add_volunteer.html

<!DOCTYPE html>
<html>
<head>
&lt;title>Add Volunteer&lt;/title>
</head> <body>
&lt;h1>Add Volunteer&lt;/h1>
&lt;form method="post">
    &lt;label>Name:&lt;/label>
    &lt;input type="text" name="name" required>
    &lt;br>
    &lt;label>Email:&lt;/label>
    &lt;input type="email" name="email" required>
    &lt;br>
    &lt;label>Phone:&lt;/label>
    &lt;input type="text" name="phone" required>
    &lt;br>
    &lt;button type="submit">Add Volunteer&lt;/button>
&lt;/form>
</body> </html>

edit_volunteer.html

<!DOCTYPE html>
<html>
<head>
&lt;title>Edit Volunteer&lt;/title>
</head> <body>
&lt;h1>Edit Volunteer&lt;/h1>
&lt;form method="post">
    &lt;label>Name:&lt;/label>
    &lt;input type="text" name="name" value="{{ volunteer.name }}" required>
    &lt;br>
    &lt;label>Email:&lt;/label>
    &lt;input type="email" name="email" value="{{ volunteer.email }}" required>
    &lt;br>
    &lt;label>Phone:&lt;/label>
    &lt;input type="text" name="phone" value="{{ volunteer.phone }}" required>
    &lt;br>
    &lt;button type="submit">Update Volunteer&lt;/button>
&lt;/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/.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *