A beginner-friendly, fully-featured social media web application built with FastAPI (Python backend), MongoDB (database), and Bootstrap 5 + HTML (frontend).
- β User Registration with validation
- β Login with JWT tokens
- β Password hashing (bcrypt)
- β Secure cookie sessions
- β Error messages for wrong credentials
- β Profile pictures (upload & change)
- β Edit profile (username, email, password)
- β Unique username validation
- β Follower/following counts and lists
- β Profile picture display everywhere
- β Create text posts
- β Upload images with posts
- β
Individual post pages (
/posts/{post_id}) - β Like/unlike toggle button
- β Comment on posts
- β Author profile pictures
- β Global feed (all posts)
- β Following filter (personalized)
- β Popular filter (most-liked)
- β Recent filter (last 24 hours)
- β Follow/unfollow users
- β Clickable follower lists
- β Clickable following lists
- β Profile pictures in lists
- β Search users by username
- β Search posts by content
- β Case-insensitive search
- β Results page with counts
- β
Admin login (
admin/admin123) - β User statistics
- β Post statistics
- β Most liked posts
- β User management (delete)
- β Post management (delete)
- β Bootstrap 5 framework
- β Bootstrap Icons (no emojis!)
- β Responsive mobile design
- β Clean color scheme
- β Smooth animations
- β Professional typography
- β MongoDB indexes
- β Query pagination
- β Image optimization
- β Fast database queries
- β Efficient aggregations
peoplesconnect_1/
βββ backend/
β βββ main.py # FastAPI routes and application
β βββ database.py # MongoDB connection and indexes
β βββ models.py # Pydantic models for validation
β βββ auth.py # Authentication utilities (JWT, password hashing)
βββ templates/
β βββ base.html # Base template with navbar/footer
β βββ navbar.html # Navigation bar component
β βββ footer.html # Footer component
β βββ index.html # Landing page
β βββ register.html # User registration
β βββ login.html # User login
β βββ feed.html # Main feed with posts
β βββ post_detail.html # Individual post page
β βββ profile.html # User profile page
β βββ admin_login.html # Admin login page
β βββ admin_dashboard.html # Admin dashboard
βββ static/
β βββ css/
β βββ style.css # Custom CSS styles
βββ requirements.txt # Python dependencies
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore rules
βββ README.md # This file
{
"_id": ObjectId,
"username": String (unique, indexed),
"email": String (unique, indexed),
"password": String (hashed),
"profile_pic": String, // NEW: URL to profile picture
"followers": [String], // Array of usernames
"following": [String], // Array of usernames
"created_at": DateTime
}{
"_id": ObjectId,
"author": String (indexed),
"content": String (max 500 chars),
"image": String, // NEW: URL to post image
"timestamp": DateTime (indexed, descending),
"likes": [String], // Array of usernames
"comments": [
{
"username": String,
"text": String (max 200 chars),
"timestamp": DateTime
}
]
}# Users collection
db.users.create_index("username", unique=True)
db.users.create_index("email", unique=True)
# Posts collection
db.posts.create_index([("timestamp", -1)]) # Sort by newest
db.posts.create_index("author") # Filter by author- Python 3.8+
- MongoDB (running on
localhost:27017)
-
Test your setup:
python test_setup.py
-
Start the application:
- Double-click
start.bat, OR - Run manually:
cd backend python -m uvicorn backend.main:app --reload --port 8000
- Double-click
-
Open browser: http://localhost:8000
pip install -r requirements.txtMake sure MongoDB is running:
# Windows (if MongoDB is installed as service)
net start MongoDB
# Linux/Mac
sudo systemctl start mongod
# or
mongod --dbpath /path/to/data/directorypython test_setup.pyThis will check all dependencies, directories, and MongoDB connection.
cd backend
python -m uvicorn main:app --reloadThe app will be available at: http://localhost:8000
- COMPLETE_GUIDE.md - Comprehensive feature documentation
- DEPLOYMENT.md - Production deployment guide
- FINAL_SUMMARY.md - Project completion summary
- QUICKSTART.md - Quick setup guide
- URL: http://localhost:8000/admin/login
- Username:
admin - Password:
admin123
ADMIN_PASSWORD_HASH in backend/main.py
| Method | Route | Description |
|---|---|---|
| GET | / |
Home page |
| GET | /register |
Registration page |
| POST | /register |
Create new user |
| GET | /login |
Login page |
| POST | /login |
User login |
| GET | /logout |
Logout user |
| Method | Route | Description |
|---|---|---|
| GET | /feed |
View personalized feed |
| POST | /posts/create |
Create new post (with optional image) |
| GET | /posts/{post_id} |
View individual post |
| POST | /posts/{post_id}/like |
Like/unlike post |
| POST | /posts/{post_id}/comment |
Add comment |
| Method | Route | Description |
|---|---|---|
| GET | /profile/{username} |
View user profile |
| POST | /follow/{username} |
Follow user |
| POST | /unfollow/{username} |
Unfollow user |
| POST | /profile/upload-picture |
Upload/change profile picture |
| Method | Route | Description |
|---|---|---|
| GET | /admin/login |
Admin login page |
| POST | /admin/login |
Admin authentication |
| GET | /admin/dashboard |
Admin dashboard |
| POST | /admin/users/{user_id}/delete |
Delete user |
| POST | /admin/posts/{post_id}/delete |
Delete post |
| GET | /admin/logout |
Admin logout |
- Login to your account
- Go to
/feed - Type your content (max 500 characters)
- Click "Post"
- Visit their profile:
/profile/{username} - Click "Follow" button
- Their posts will appear in your feed
- Login at
/admin/login - View statistics on dashboard
- Manage users and posts
- Delete inappropriate content
-
Indexes: Added indexes on frequently queried fields
usernameandemailfor user lookupstimestampfor sorting postsauthorfor filtering posts
-
Projection: Only fetch required fields
# Example: Exclude password from user queries db.users.find({}, {"password": 0})
-
Pagination: Limit results to prevent large data transfers
# Limit to 50 posts db.posts.find().limit(50)
-
Aggregation Pipeline: Efficient statistics queries
# Get most liked posts db.posts.aggregate([ {"$project": {"like_count": {"$size": "$likes"}}}, {"$sort": {"like_count": -1}}, {"$limit": 5} ])
| Component | Technology |
|---|---|
| Backend | FastAPI (Python) |
| Database | MongoDB (Motor async driver) |
| Frontend | Bootstrap 5 + HTML + Jinja2 |
| Authentication | JWT (python-jose) |
| Password Hashing | Bcrypt |
| Image Processing | Pillow (PIL) |
| Server | Uvicorn (ASGI) |
# backend/main.py
@app.get("/custom-route")
async def custom_route(request: Request):
username = get_current_user(request)
if not username:
return RedirectResponse("/login")
# Your logic here
return templates.TemplateResponse("custom.html", {
"request": request,
"username": username
})db = get_database()
# Find all posts by a user
posts = await db.posts.find({"author": username}).to_list(100)
# Update a post
await db.posts.update_one(
{"_id": ObjectId(post_id)},
{"$set": {"content": new_content}}
)
# Delete a post
await db.posts.delete_one({"_id": ObjectId(post_id)})- Ensure MongoDB is running:
mongod --version - Check connection string in
backend/database.py
# Change port when starting
uvicorn main:app --port 8001# Reinstall dependencies
pip install -r requirements.txtThis project is created for educational purposes. Feel free to modify and use it for learning.
uvicorn main:app --reload --host 0.0.0.0 --port 8000# Use MongoDB Shell
mongosh
use peopleconnects
db.users.find()
db.posts.find().sort({timestamp: -1})// In MongoDB Shell
use peopleconnects
db.users.deleteMany({})
db.posts.deleteMany({})For issues or questions:
- Check this README thoroughly
- Review the code comments in
backend/files - Verify MongoDB is running and accessible
- Check FastAPI logs in terminal
Happy Coding! π