One Blog, Three Frameworks: Routing, Models, Templates and Layout in Django, FastAPI and Flask
The same blog built in Django, FastAPI and Flask, compared line by line: the list route, the Post model, the template loop, the project layout and the extras, with a ROSETTA map you can use to port a feature with AI.
What you will learn
- Find the list-posts route, the Post model and the template loop in all three blogs
- Map Django names to their FastAPI and Flask equivalents with a file-and-line table
- Tell which extras are built in, come from a package or were written by hand
- Use a feature map as context and as a review checklist when porting with AI
Before you start
- One finished blog in Django, FastAPI or Flask
- Python classes and decorators
You built the blog in one framework, and that's the right way to learn. But sooner or later you meet another one: a new job where the team uses a different framework, a teammate who shares code in another one, or a client whose app already runs on one. The good news is that the features stay the same. Only the names and the places change. All you need is a map.
Think of one recipe cooked in three kitchens. The recipe is short: list the posts, store the posts, show a page, let people log in. Django is a fitted kitchen: the oven and cupboards are built in, and each thing goes where the manual says. FastAPI is a kitchen with labelled drawers, and the menu prints itself. Flask is an open kitchen: very little is built in, so you bring your own pans. Learn the recipe once, then learn where each kitchen keeps things.
This article compares the three finished blogs from the course tracks (the S01L05 projects with templates). Every file and line below comes from a map called ROSETTA.md that was checked against the real files.
Routing: an address, a read, a render
Routing answers one question: when someone opens the home address, which code runs?
FastAPI
app/routes/posts.py:
router = APIRouter()
templates = Jinja2Templates(directory="app/templates")
@router.get("/", response_class=HTMLResponse)
async def index(request: Request, db: Session = Depends(get_db)):
posts = db.query(Post).order_by(Post.created_at.desc()).all()
user = get_current_user(request, db)
return templates.TemplateResponse(request=request, name="index.html", context={"posts": posts, "user": user})
Line 13, @router.get("/"), is the address, with index below it. Line 15 reads the posts newest first, and line 17 hands them to the template. In app/main.py, line 40 plugs the router into the app: app.include_router(posts.router, tags=["posts"]).
Django
Django splits the work across two files. blog/urls.py line 5 sends the empty address to the view:
path('', views.home, name='home'),
and blog/views.py reads the published posts (line 14) and renders the page (line 30):
def home(request):
"""Home page displaying all published posts"""
posts_list = Post.objects.filter(status='published')
...
# Pagination
paginator = Paginator(posts_list, 6) # 6 posts per page
page_number = request.GET.get('page')
posts = paginator.get_page(page_number)
return render(request, 'blog/home.html', {'posts': posts, 'search_query': search_query})
The project's blogproject/urls.py line 25 plugs the app in with path('', include('blog.urls')).
Flask
app/routes/blog.py:
bp = Blueprint('blog', __name__)
...
@bp.route('/')
def index():
page = request.args.get('page', 1, type=int)
posts = Post.query.order_by(Post.created_at.desc()).paginate(
page=page, per_page=10, error_out=False)
return render_template('index.html', posts=posts)
A blueprint is Flask's group of routes. Line 17 is the address, line 20 reads the posts newest first, line 22 renders the page, and app/__init__.py line 47 registers the blueprint with app.register_blueprint(blog.bp).
The Post model
FastAPI, app/models.py from line 21, built on SQLAlchemy's Base:
class Post(Base):
__tablename__ = "posts"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, nullable=False)
content = Column(Text, nullable=False)
author_id = Column(Integer, ForeignKey("users.id"), nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
Django, blog/models.py from line 17, built on models.Model (choice lists left out):
class Post(models.Model):
...
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True, blank=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
content = models.TextField()
excerpt = models.TextField(max_length=500, blank=True, help_text='Brief summary of the post')
category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='Other')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
Django builds the table from these fields with a migration. Flask, app/models.py from line 30, built on db.Model:
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), nullable=False)
slug = db.Column(db.String(200), unique=True, nullable=False, index=True)
content = db.Column(db.Text, nullable=False)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
Flask reads almost like FastAPI, because both sit on SQLAlchemy. Every post has an id, a title, the content and an author. Only the spelling changes.
Templates: a loop, a link and a title
| Loop | Link to one post | |
|---|---|---|
FastAPI index.html | line 10: {% for post in posts %} | line 12: <a href="/post/{{ post.id }}">{{ post.title }}</a> |
Django home.html | line 71: {% for post in posts %} | line 88: <a href="{% url 'post_detail' post.slug %}" ...>{{ post.title }}</a> |
Flask index.html | line 53: {% for post in posts.items[1:] %} | line 63: <a href="{{ url_for('blog.post_detail', slug=post.slug) }}" ...>, title on line 64 |
FastAPI types the link out by hand: /post/ plus the numeric id. This is Jinja2. Django's url tag looks the route up by its name, with the post's slug. Flask's url_for also finds the route by its name, and it's the same Jinja2 as FastAPI. Flask's loop starts from the second post because the first one is shown above it as the featured post. All three pages start by extending a layout, {% extends "base.html" %} (Django: 'blog/base.html').
Project layout
Seed scripts, tests and migrations left out:
- FastAPI: one
appfolder withmodels.py,routes/andtemplates/, next tomain.py. - Django: split by app. The
blogapp has its models, views, URLs and its owntemplatesfolder.blogprojectholds the settings, andmanage.pyruns everything. - Flask: one
appfolder with models, routes and templates;config.pyholds the settings andmain.pystarts it.
Django makes you split by app; FastAPI and Flask let you choose. All three end up with models, routes and templates.
The extras each blog has
| Extra | FastAPI | Django | Flask |
|---|---|---|---|
| Admin | written by us: require_admin, app/auth.py:93 | built in: path('admin/', admin.site.urls), blogproject/urls.py:24 | written by us: admin_required, app/routes/admin.py:14 |
| Login | written by us on SessionMiddleware, app/main.py:33 | built in: login_required, blog/views.py:3 | package: Flask-Login LoginManager(), app/__init__.py:11 |
| Forms | built in: title: str = Form(...), app/routes/posts.py:44 | built in: class PostForm(forms.ModelForm), blog/forms.py:43 | package: Flask-WTF FlaskForm, app/routes/admin.py:25 |
| API docs | built in: docs_url="/docs", app/main.py:19 | package: drf-spectacular, blogproject/urls.py:30 | package: Swagger UI blueprint, app/__init__.py:36 |
Count them: five built in, four from a package, three written by hand. Knowing which one it is tells you where to look.
Proof: each list page answers 200
Each blog was started from a temporary copy, asked for the list page once, and stopped. The recorded run (run_all.md):
| Blog | Version | GET / | Page title | Post links | Other checks |
|---|---|---|---|---|---|
| FastAPI | 0.136.1 | 200 | Home - Blog | 6 | /docs 200 |
| Django | 6.1.3 | 200 | Home - Elegant Blog | 6 | /admin/login/ 200, /api/docs/ 200 |
| Flask | 3.1.3 | 200 | Home - Blog | 10 | /api/docs/ 200 |
Why ten in Flask? Page size. Flask shows ten posts per page (per_page=10, line 21 of blog.py), Django's paginator shows six, and FastAPI lists all of its six posts.
The map, and porting with AI
ROSETTA.md has one row per feature, with the file and line in all three blogs. A few rows:
| Feature | FastAPI | Django | Flask |
|---|---|---|---|
| List-posts route | app/routes/posts.py:13 | blog/urls.py:5 | app/routes/blog.py:17 |
| The Post model | app/models.py:21 | blog/models.py:17 | app/models.py:30 |
| Loop over the posts | app/templates/index.html:10 | blog/templates/blog/home.html:71 | app/templates/index.html:53 |
| Login support | app/main.py:33 | blog/views.py:3 | app/__init__.py:11 |
| API docs | app/main.py:19 | blogproject/urls.py:30 | app/__init__.py:36 |
To port a blog with AI, start from the map. Take the list-posts row: the Django column says where it is now, the Flask column says where it should go. Five rows become five small jobs you can check, instead of "rewrite my blog". A sample prompt from the lesson's PROMPT.md:
I have a blog written in Django and I want the same blog in Flask.
Use ROSETTA.md as the map. For the list-posts page, find the Django
file and line in each row, then write the Flask version in the place
the Flask column names.
Keep the behaviour: newest post first, and each title links to its post.
Show the result file by file, and list anything the Flask project does
not have yet.
When the answer comes back, check three things. Does every row of the map have a matching change? Do the route names fit the new framework, like url_for or the url tag? Does the list page answer 200? The map is also your review checklist.
Quick recap
| Concept | FastAPI | Django | Flask |
|---|---|---|---|
| Group of routes | APIRouter | app urls.py | Blueprint |
| Model base | SQLAlchemy Base | models.Model | Flask-SQLAlchemy db.Model |
| Query | db.query(Post) | Post.objects | Post.query |
| Templates | Jinja2 | Django templates | Jinja2 |
| Link by route name | (typed by hand here) | {% url %} | url_for |
If you haven't picked a track yet, start with Django, FastAPI or Flask.
Try it yourself
Find the list route of your blog in the map, open the same row in another track, and port just that one route, by hand or with the prompt above. Then send one GET request to the new list page.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Django, FastAPI or Flask? The Same Blog Feature in Three Shapes, and How to Choose
One small project lists two blog posts three times: as a Django view, a Flask route and a FastAPI endpoint. See each shape with its real output, what each framework includes, and a three-question way to choose.
Getting Started with Django: Models, Views, Templates, the ORM and the Admin
A tiny Django menu app with one model, one view, one URL, one template and the admin. What each file does, how one request travels through them, and the real output of a run.
Your First Django Project and App: uv, startproject, startapp and a First View
Set up a Django project called blogproject with uv, add an app called blog, write one view that returns Hello blog, give it the empty path and run the server. Every command with its real output.
The Django Project Folder Structure, File by File: Reading a Blog an AI Agent Built
A tour of a finished Django blog: manage.py, the project package, the blog app with its models, views, templates, forms and admin, a custom command, the REST API and Swagger docs, plus the review points check --deploy found.
