An open API service indexing awesome lists of open source software.

https://github.com/shahidmalik4/fastapi-html

A lightweight blog application built with FastAPI, supporting user authentication, HTML server-side rendering, and full CRUD functionality for blog posts.
https://github.com/shahidmalik4/fastapi-html

blog fastapi html python sqlalchemy

Last synced: 5 days ago
JSON representation

A lightweight blog application built with FastAPI, supporting user authentication, HTML server-side rendering, and full CRUD functionality for blog posts.

Awesome Lists containing this project

README

          

# ๐Ÿ“ FastAPI Blog App with Server-Side HTML Rendering

[![Python](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/)
[![FastAPI](https://img.shields.io/badge/FastAPI-0.110+-green.svg)](https://fastapi.tiangolo.com/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
[![Status](https://img.shields.io/badge/status-active-brightgreen.svg)](#)

A full-featured blog application built with **FastAPI**, **Jinja2**, and **SQLAlchemy**. Supports server-side HTML rendering, user authentication, flash messaging, and full CRUD functionality โ€” all with clean modular code and session-based route protection.

---

## ๐Ÿš€ Features

- ๐Ÿ” User registration, login, and logout
- โšก Session-based authentication using secure cookies
- ๐Ÿ’ฌ Flash messages for user feedback
- ๐Ÿ“ Create, Read, Update, Delete (CRUD) blog posts
- ๐Ÿ”— Slug generation for posts
- ๐Ÿงฐ FastAPI + Jinja2 HTML templating
- ๐Ÿงฑ SQLite database (easy to swap out)
- ๐Ÿ›ก๏ธ Protected routes (dashboard, create post, etc.)
- ๐Ÿ—ƒ๏ธ Clean, modular architecture (auth, crud, schemas, models)
- ๐Ÿš€ Deployable on **Render**, **Heroku**, or **any ASGI server**

---

## ๐Ÿงฐ Tech Stack

- **Framework**: [FastAPI](https://fastapi.tiangolo.com/)
- **ORM**: SQLAlchemy
- **Templates**: Jinja2
- **Schema Validation**: Pydantic
- **Database**: SQLite (can be upgraded to PostgreSQL/MySQL)
- **Web Server**: Uvicorn

---

## ๐Ÿ“ Project Structure

```
fastapi-html/
โ”œโ”€โ”€ app/
โ”‚ โ”œโ”€โ”€ auth.py # Auth routes (register, login, logout)
โ”‚ โ”œโ”€โ”€ crud.py # Post/user CRUD logic
โ”‚ โ”œโ”€โ”€ database.py # SQLAlchemy DB connection
โ”‚ โ”œโ”€โ”€ deps.py # Dependency overrides and helpers
โ”‚ โ”œโ”€โ”€ main.py # App entry point
โ”‚ โ”œโ”€โ”€ models.py # SQLAlchemy models
โ”‚ โ”œโ”€โ”€ schemas.py # Pydantic schemas
โ”‚ โ”œโ”€โ”€ templates/ # HTML templates
โ”‚ โ””โ”€โ”€ utils.py # Flash messaging & helpers
โ”œโ”€โ”€ blog_app.db # SQLite DB file
โ”œโ”€โ”€ requirements.txt # Python dependencies
โ”œโ”€โ”€ render.yml # Render deployment config
โ””โ”€โ”€ README.md # You're reading it!
```

---

## ๐Ÿ“ฆ Installation

```bash
# Clone the repository
git clone https://github.com/shahidmalik4/fastapi-html.git
cd fastapi-html

# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run the server
uvicorn app.main:app --reload
```

---

## ๐Ÿ” Auth Flow

- `/register`: Register new user
- `/login`: Login page (sets session)
- `/logout`: Logout and clear session
- Routes like `/dashboard` and `/create-post` are protected and require login

---

## ๐Ÿง‘โ€๐Ÿ’ป CRUD Endpoints

| Route | Method | Auth Required | Description |
|-------------------|--------|---------------|--------------------------|
| `/register` | GET/POST | โŒ | Register a new user |
| `/login` | GET/POST | โŒ | Login with credentials |
| `/logout` | GET | โœ… | Logout user |
| `/dashboard` | GET | โœ… | View user dashboard |
| `/create-post` | GET/POST | โœ… | Create a new post |
| `/edit-post/{id}` | GET/POST | โœ… | Edit an existing post |
| `/delete-post/{id}`| GET | โœ… | Delete a post |
| `/post/{slug}` | GET | โœ… | View a single post |

---

## ๐Ÿงพ Models

```python
๐Ÿง User (SQLAlchemy)
๐Ÿ“ Post (SQLAlchemy)
```

---

## ๐Ÿ“ฆ Pydantic Schemas

```python
class UserCreate(BaseModel):
class UserOut(BaseModel):
class PostBase(BaseModel):
class PostCreate(PostBase):
class PostOut(PostBase):
```

---

## ๐Ÿ“ค Deployment (Render Example)

`render.yml` already included for Render deployment.

> You can also deploy using Docker, Heroku, or any ASGI-compatible cloud.

---