This is a full-stack blog website with:
- Backend: FastAPI with SQLAlchemy ORM
- Frontend: React with React Router
- Database: SQLite (easily switchable to PostgreSQL)
- Authentication: JWT Token-based
- Roles: Admin, Author, Reader
- Browse all published blogs
- Read blog posts with author information
- User authentication
- Create and upload blogs
- Edit their own blogs
- Publish/unpublish blogs
- View all their published blogs
- View all blogs and users
- Delete blogs
- Create and manage notices/announcements
- Delete user accounts if needed
- FastAPI - Modern Python web framework
- SQLAlchemy - ORM for database operations
- Pydantic - Data validation
- SQLite - Lightweight database
- JWT - Authentication tokens
- Passlib & Bcrypt - Password hashing
- React 18 - UI library
- React Router DOM - Client-side routing
- Axios - HTTP client
- CSS - Styling
Blog-website/
βββ backend/
β βββ app/
β β βββ models/
β β β βββ __init__.py
β β β βββ user.py # User model
β β β βββ blog.py # Blog model
β β β βββ notice.py # Notice model
β β βββ routes/
β β β βββ users.py # User API endpoints
β β β βββ blogs.py # Blog API endpoints
β β β βββ notices.py # Notice API endpoints
β β βββ schemas/
β β β βββ user.py # User validation schemas
β β β βββ blog.py # Blog validation schemas
β β β βββ notice.py # Notice validation schemas
β β βββ database/
β β β βββ database.py # DB configuration
β β βββ auth.py # Authentication utilities
β βββ main.py # FastAPI app entry
β βββ requirements.txt # Python dependencies
β βββ .env # Environment variables
β
βββ frontend/
βββ public/
β βββ index.html
βββ src/
β βββ components/
β β βββ Header.js
β β βββ BlogCard.js
β βββ pages/
β β βββ Home.js
β β βββ Login.js
β β βββ Register.js
β β βββ BlogDetail.js
β β βββ CreateBlog.js
β β βββ Admin.js
β βββ services/
β β βββ api.js # API client
β βββ context/
β β βββ AuthContext.js # Auth state management
β βββ styles/
β β βββ index.css
β β βββ Header.css
β β βββ Auth.css
β β βββ Home.css
β β βββ Blog.css
β β βββ CreateBlog.css
β β βββ Admin.css
β βββ App.js
β βββ index.js
βββ package.json
- Python 3.8+
- Node.js 14+
- npm or yarn
cd Blog-website/backendpython -m venv venv# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activatepip install -r requirements.txtpython main.pyThe backend will start at http://localhost:8000
API Documentation: Visit http://localhost:8000/docs (Swagger UI)
cd Blog-website/frontendnpm installnpm startThe frontend will open at http://localhost:3000
- Click "Register" on the home page
- Enter username, email, full name, and password
- Choose role: Reader or Author
- Click Register
- Click "Login" on the home page
- Enter username and password
- Token is saved to localStorage
- Browse Blogs: Visit homepage to see all published blogs
- Read Blog: Click "Read More" on any blog card to view full content
- View Author: See author info on blog detail page
- Register with "Author" role
- Click "Write Blog" in navigation
- Fill in title, description, and content
- Click "Publish Blog"
- Your blog appears on homepage
- Register first as normal user (or backend admin setup)
- Click "Admin" in navigation
- Manage Blogs: View and delete any blog
- Create Notices: Post announcements for users
- View Users: See all registered users
POST /api/users/register- Register new userPOST /api/users/login- User loginGET /api/users/me- Get current user (requires auth)GET /api/users/- List all usersGET /api/users/{user_id}- Get user detailsPUT /api/users/{user_id}- Update userDELETE /api/users/{user_id}- Delete user (admin only)
POST /api/blogs/- Create blog (author/admin only)GET /api/blogs/- List published blogsGET /api/blogs/{blog_id}- Get blog detailsPUT /api/blogs/{blog_id}- Update blog (author/admin)DELETE /api/blogs/{blog_id}- Delete blog (author/admin)GET /api/blogs/author/{author_id}- Get author's blogs
POST /api/notices/- Create notice (admin only)GET /api/notices/- List all noticesGET /api/notices/{notice_id}- Get notice detailsPUT /api/notices/{notice_id}- Update notice (admin only)DELETE /api/notices/{notice_id}- Delete notice (admin only)
To manually create an admin user, run Python shell in backend:
from app.database.database import SessionLocal
from app.models.user import User, UserRole
from app.auth import get_password_hash
db = SessionLocal()
admin = User(
username="admin",
email="admin@example.com",
full_name="Admin User",
hashed_password=get_password_hash("admin123"),
role=UserRole.ADMIN
)
db.add(admin)
db.commit()
print("Admin user created!")- id: Integer (PK)
- username: String
- email: String
- full_name: String
- hashed_password: String
- role: Enum (admin, author, reader)
- is_active: Boolean
- created_at: DateTime
- updated_at: DateTime
- id: Integer (PK)
- title: String
- content: Text
- description: String
- author_id: Foreign Key
- is_published: Boolean
- created_at: DateTime
- updated_at: DateTime
- id: Integer (PK)
- title: String
- content: Text
- sender_id: Foreign Key
- created_at: DateTime
- updated_at: DateTime
- Register as Author
- Go to "Write Blog"
- Fill in details and publish
- Blog automatically appears on homepage
- Login as Admin
- Go to Admin Panel
- View all blogs
- Click Delete on any blog
- Visit homepage (no login needed)
- See all published blogs
- Click any blog to read full content
- See author details
DATABASE_URL=sqlite:///./blog.db
SECRET_KEY=your-secret-key-change-in-production
For Production Database (PostgreSQL):
DATABASE_URL=postgresql://user:password@localhost/blog_db
Edit frontend/src/services/api.js:
const API_BASE_URL = 'http://localhost:8000/api';Port 8000 already in use
# Change port in main.py or use:
uvicorn app.main:app --port 8001Database errors
# Delete SQLite file and restart
rm blog.db
python main.pyPort 3000 already in use
# Use different port:
PORT=3001 npm startCORS errors
- Backend CORS is enabled for all origins
- Check that frontend URL matches in settings
pip install gunicorn
gunicorn -w 4 -k uvicorn.workers.UvicornWorker app.main:appnpm run buildFeel free to extend this project with:
- Comments on blogs
- Search functionality
- Categories/Tags
- User profiles
- Email notifications
- Image uploads
This project is open source and available under MIT License.
For issues or questions, please check the code comments or reach out!
Happy blogging! πβ¨