Pet Adoption Platform

Project Structure

pet_adoption/
│
├── app.py                # Main application file
├── templates/            # HTML templates
│   ├── index.html
│   ├── add_pet.html
│   └── pet_details.html
├── static/               # Static files (CSS, JS, images)
│   └── styles.css
└── pets.db               # SQLite database

Step 1: Set Up the Environment

  1. Install Flask:bashCopy codepip install Flask
  2. Set up SQLite (it usually comes with Python, but you can install it if necessary):bashCopy codepip install sqlite3

Step 2: Create the Database

Create a file named database.py to set up the SQLite database:

import sqlite3

def init_db():
conn = sqlite3.connect('pets.db')
c = conn.cursor()
c.execute('''
    CREATE TABLE IF NOT EXISTS pets (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        breed TEXT NOT NULL,
        age INTEGER NOT NULL,
        description TEXT NOT NULL
    )
''')
conn.commit()
conn.close()
if __name__ == '__main__':
init_db()

Run this file once to create the database.

Step 3: Create the Main Application

Create a file named app.py:

from flask import Flask, render_template, request, redirect
import sqlite3

app = Flask(__name__)

# Database helper functions
def get_db_connection():
conn = sqlite3.connect('pets.db')
conn.row_factory = sqlite3.Row
return conn
@app.route('/') def index():
conn = get_db_connection()
pets = conn.execute('SELECT * FROM pets').fetchall()
conn.close()
return render_template('index.html', pets=pets)
@app.route('/add', methods=('GET', 'POST')) def add_pet():
if request.method == 'POST':
    name = request.form['name']
    breed = request.form['breed']
    age = request.form['age']
    description = request.form['description']
    conn = get_db_connection()
    conn.execute('INSERT INTO pets (name, breed, age, description) VALUES (?, ?, ?, ?)',
                 (name, breed, age, description))
    conn.commit()
    conn.close()
    return redirect('/')

return render_template('add_pet.html')
@app.route('/pet/<int:pet_id>') def pet_details(pet_id):
conn = get_db_connection()
pet = conn.execute('SELECT * FROM pets WHERE id = ?', (pet_id,)).fetchone()
conn.close()
return render_template('pet_details.html', pet=pet)
if __name__ == '__main__':
app.run(debug=True)

Step 4: Create HTML Templates

1. templates/index.html:

<!doctype html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
&lt;link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
&lt;title>Pet Adoption&lt;/title>
</head> <body>
&lt;h1>Pet Adoption Platform&lt;/h1>
&lt;a href="/add">Add a Pet&lt;/a>
&lt;h2>Available Pets&lt;/h2>
&lt;ul>
    {% for pet in pets %}
        &lt;li>
            &lt;a href="{{ url_for('pet_details', pet_id=pet&#91;'id']) }}">{{ pet&#91;'name'] }} ({{ pet&#91;'breed'] }})&lt;/a>
        &lt;/li>
    {% endfor %}
&lt;/ul>
</body> </html>

2. templates/add_pet.html:

<!doctype html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
&lt;title>Add a Pet&lt;/title>
</head> <body>
&lt;h1>Add a Pet&lt;/h1>
&lt;form method="post">
    &lt;label>Name:&lt;/label>&lt;br>
    &lt;input type="text" name="name" required>&lt;br>
    &lt;label>Breed:&lt;/label>&lt;br>
    &lt;input type="text" name="breed" required>&lt;br>
    &lt;label>Age:&lt;/label>&lt;br>
    &lt;input type="number" name="age" required>&lt;br>
    &lt;label>Description:&lt;/label>&lt;br>
    &lt;textarea name="description" required>&lt;/textarea>&lt;br>
    &lt;input type="submit" value="Add Pet">
&lt;/form>
</body> </html>

3. templates/pet_details.html:

<!doctype html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
&lt;title>{{ pet&#91;'name'] }}&lt;/title>
</head> <body>
&lt;h1>{{ pet&#91;'name'] }}&lt;/h1>
&lt;p>Breed: {{ pet&#91;'breed'] }}&lt;/p>
&lt;p>Age: {{ pet&#91;'age'] }} years&lt;/p>
&lt;p>Description: {{ pet&#91;'description'] }}&lt;/p>
&lt;a href="/">Back to Home&lt;/a>
</body> </html>

Step 5: Create a CSS File

Create a file named static/styles.css for basic styling:

body {
font-family: Arial, sans-serif;
margin: 20px;
} h1 {
color: #333;
} a {
text-decoration: none;
color: blue;
} ul {
list-style-type: none;
padding: 0;
}

Step 6: Run the Application

  1. Initialize the database:bashCopy codepython database.py
  2. Run the Flask app:
python app.py
  1. Open your browser and go to http://127.0.0.1:5000/.

Comments

Leave a Reply

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