Fastapi Blog Project Init
This chapter takes you from scratch to create your first Web application with FastAPI and experience the auto-generated API documentation.
* * *
## FastAPI Positioning Difference from Django/Flask
FastAPI is a new star among Python Web frameworks, positioned between Django and Flask.
| Feature | Django | Flask | FastAPI |
| --- | --- | --- | --- |
| Design Philosophy | Batteries included | Microframework, extend as needed | High performance, type-driven |
| Async Support | Supported from 3.1+ | No native support | Native async/await |
| API Docs | Requires drf-spectacular | Requires Flasgger | Auto-generated Swagger UI |
| Data Validation | Django Forms / DRF Serializer | Flask-WTF / Manual | Pydantic (built-in) |
| Server | WSGI (Gunicorn) | WSGI (Gunicorn) | ASGI (Uvicorn) |
| Type Safety | Optional | Optional | Mandatory Python type hints |
The biggest difference with FastAPI: everything is driven by Python type hints β route parameters are automatically validated, request/response is automatically serialized, and interactive documentation is automatically generated.
* * *
## Environment Preparation
$ python3 -m venv venv $ source venv/bin/activate (venv) $ pip install fastapi uvicorn jinja2 python-multipart
Explanation of three core dependencies:
* fastapi: The Web framework itself
* uvicorn: ASGI server (equivalent to Flask's Gunicorn)
* jinja2: Template engine (optional, but needed for SSR)
* * *
## Minimal FastAPI Application
## Example
# File path: main.py
from fastapi import FastAPI
# Create FastAPI instance
app = FastAPI(
title="TUTORIAL Blog",
description="Personal blog showcase built with FastAPI",
version="1.0.0"
)
@app.get("/")# GET request route decorator
def index():
"""Homepage"""
return{"message": "Welcome to TUTORIAL Blog"}
@app.get("/hello/{name}")# Path parameter {name}
def hello(name: str): # FastAPI automatically validates based on type hint
return{"greeting": f"Hello, {name}!"}
### Starting the Server
(venv) $ uvicorn main:app --reload INFO: Uvicorn running on http://127.0.0.1:8000
Note the different startup method from Flask:
* Flask: `flask run` (based on Werkzeug)
* FastAPI: `uvicorn main:app` (based on ASGI, native async support)
* * *
## Auto-Generated API Documentation
This is the most amazing feature of FastAPI β without writing any extra code, it automatically generates interactive documentation.
Visit `/docs` to see the Swagger UI, where you can test the API directly on the page:
http://127.0.0.1:8000/docs # Swagger UI (interactive testing) http://127.0.0.1:8000/redoc # ReDoc (read-only documentation, more beautiful)
In Swagger UI: click GET /hello/{name} β Try it out β enter name β Execute, and you can see the real response.
* * *
## Project Directory Structure Planning
blog_project/βββ main.py # Application entryβββ models.py # SQLAlchemy ORM modelsβββ schemas.py # Pydantic Schema (unique to FastAPI)βββ database.py # Database connection and Sessionβββ auth.py # JWT authentication logicβββ routers/ # APIRouter route modulesβ βββ posts.py β βββ categories.py β βββ users.py β βββ favorites.py βββ templates/ # Jinja2 templatesβββ static/ # Static filesβββ alembic/ # Database migrations
> Flask uses Blueprint to organize routes, FastAPI uses APIRouter. The concepts are similar but the syntax is different, and APIRouter naturally carries type constraints.
* * *
## Chapter Summary
In this chapter, you completed three things: installed FastAPI + Uvicorn, wrote your first route with type hints, and visited the auto-generated /docs interactive API documentation.
Core takeaway: FastAPI uses Python type hints to drive parameter validation and documentation generation β this is the most essential characteristic that distinguishes it from Django/Flask.
YouTip