<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Linh Fish Dev</title>
    <description>The latest articles on DEV Community by Linh Fish Dev (@linhfishdev).</description>
    <link>https://dev.to/linhfishdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F653872%2F583ca1a7-b675-40b3-a272-07298d94bb05.png</url>
      <title>DEV Community: Linh Fish Dev</title>
      <link>https://dev.to/linhfishdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/linhfishdev"/>
    <language>en</language>
    <item>
      <title>Django: Check Security Before Deploying with manage.py check –deploy in 5 Minutes</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Wed, 25 Nov 2026 15:13:37 +0000</pubDate>
      <link>https://dev.to/linhfishdev/django-check-security-before-deploying-with-managepy-check-deploy-in-5-minutes-1cpe</link>
      <guid>https://dev.to/linhfishdev/django-check-security-before-deploying-with-managepy-check-deploy-in-5-minutes-1cpe</guid>
      <description>&lt;p&gt;Before shipping a Django project to production, run python manage.py check –deploy: it scans your settings file and lists security holes such as DEBUG = True, a SECRET_KEY that is too short, missing HTTPS, or cookies without the Secure flag. The command ships with every Django project and finishes in seconds — but it is only accurate when run against your production settings. Running it on dev settings gives misleading results.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Production settings mistakes rarely crash the app, so they slip through easily. A freshly created project, deployed as-is without a settings review, carries a whole set of dev defaults: DEBUG = True, a short default SECRET_KEY, an empty ALLOWED_HOSTS, and missing security middleware. The result: error pages that expose tracebacks and environment variables, session cookies sent over plain HTTP, and browsers that do not block your pages from being embedded in a stranger’s iframe.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# settings.py — dev config shipped straight to production&lt;/span&gt;
&lt;span class="no"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"short-key"&lt;/span&gt;
&lt;span class="no"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="no"&gt;ALLOWED_HOSTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="no"&gt;MIDDLEWARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"django.contrib.sessions.middleware.SessionMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.middleware.common.CommonMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.middleware.csrf.CsrfViewMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.contrib.auth.middleware.AuthenticationMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.contrib.messages.middleware.MessageMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the check against the correct production settings file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;deploy&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;myproject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;settings_prod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the dev settings above left untouched, Django 6.1 returns 7 warnings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;WARNINGS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;W001&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;You&lt;/span&gt; &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="s1"&gt;'django.middleware.security.SecurityMiddleware'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="no"&gt;MIDDLEWARE&lt;/span&gt; &lt;span class="n"&gt;so&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="no"&gt;SECURE_HSTS_SECONDS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SECURE_CONTENT_TYPE_NOSNIFF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SECURE_REFERRER_POLICY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SECURE_CROSS_ORIGIN_OPENER_POLICY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="no"&gt;SECURE_SSL_REDIRECT&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;W002&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;You&lt;/span&gt; &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="s1"&gt;'django.middleware.clickjacking.XFrameOptionsMiddleware'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="no"&gt;MIDDLEWARE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;so&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;pages&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;served&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="s1"&gt;'x-frame-options'&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;W009&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Your&lt;/span&gt; &lt;span class="no"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;less&lt;/span&gt; &lt;span class="n"&gt;than&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="n"&gt;characters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;less&lt;/span&gt; &lt;span class="n"&gt;than&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="n"&gt;unique&lt;/span&gt; &lt;span class="n"&gt;characters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="s1"&gt;'s prefixed with '&lt;/span&gt;&lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;insecure&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="s1"&gt;' indicating that it was generated automatically by Django.
?: (security.W012) SESSION_COOKIE_SECURE is not set to True. Using a secure-only session cookie makes it more difficult for network traffic sniffers to hijack user sessions.
?: (security.W016) You have '&lt;/span&gt;&lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csrf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CsrfViewMiddleware&lt;/span&gt;&lt;span class="s1"&gt;' in your MIDDLEWARE, but you have not set CSRF_COOKIE_SECURE to True.
?: (security.W018) You should not have DEBUG set to True in deployment.
?: (security.W020) ALLOWED_HOSTS must not be empty in deployment.

System check identified 7 issues (0 silenced).
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Fix each group of warnings: enable the security middleware, turn off DEBUG, set a long key, enforce HTTPS and Secure cookies. Generate a fresh key with get_random_secret_key(), then read it from an environment variable instead of hardcoding it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="no"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"SECRET_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
&lt;span class="no"&gt;ALLOWED_HOSTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"example.com"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="no"&gt;MIDDLEWARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"django.middleware.security.SecurityMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.contrib.sessions.middleware.SessionMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.middleware.common.CommonMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.middleware.csrf.CsrfViewMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.contrib.auth.middleware.AuthenticationMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.contrib.messages.middleware.MessageMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"django.middleware.clickjacking.XFrameOptionsMiddleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="no"&gt;SECURE_HSTS_SECONDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;31536000&lt;/span&gt;
&lt;span class="no"&gt;SECURE_HSTS_INCLUDE_SUBDOMAINS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="no"&gt;SECURE_HSTS_PRELOAD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="no"&gt;SECURE_SSL_REDIRECT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="no"&gt;SESSION_COOKIE_SECURE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="no"&gt;CSRF_COOKIE_SECURE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the check again and Django reports a clean bill of health:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt; &lt;span class="n"&gt;identified&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;issues&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;silenced&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to note. First, the HSTS (W004) and SSL redirect (W008) warnings only appear after you have added SecurityMiddleware — without it, Django reports only W001 and skips that whole group. Second, only enable HSTS once the entire site runs stably on HTTPS, because browsers remember the policy and force HTTPS for the duration you set; the Django docs warn that enabling HSTS carelessly can have serious, hard-to-reverse consequences.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Add this command to your CI/CD pipeline, run it before every deploy, and always point it at the right production settings file with –settings or the DJANGO_SETTINGS_MODULE variable — running it on dev settings and feeling safe is the most common mistake. Keep dev and prod settings in two separate files, and treat “0 issues” as a precondition before opening the gates to real users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/05/django-write-custom-middleware-to-measure-request-time-in-5-minutes/" rel="noopener noreferrer"&gt;Django: Write Custom Middleware to Measure Request Time in 5 Minutes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/16/flask-add-security-headers-to-every-response-with-after_request-in-5-minutes/" rel="noopener noreferrer"&gt;Flask: Add Security Headers to Every Response with after_request in 5 Minutes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/10/11/fastapi-cau-hinh-cors-dung-cach-bang-corsmiddleware-trong-5-phut/" rel="noopener noreferrer"&gt;FastAPI: cấu hình CORS đúng cách bằng CORSMiddleware trong 5 phút&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How is check –deploy different from a plain check?
&lt;/h3&gt;

&lt;p&gt;A plain check scans for general configuration errors. The –deploy option enables the extra security.* checks, which only make sense in production — for example DEBUG, SECRET_KEY, HSTS, and Secure cookies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I run check –deploy against dev settings?
&lt;/h3&gt;

&lt;p&gt;You can, but the results are noisy: it flags things that are perfectly fine in dev, such as DEBUG = True or an empty ALLOWED_HOSTS. For results you can act on, always run it with the production settings file via –settings or DJANGO_SETTINGS_MODULE.&lt;/p&gt;

&lt;h3&gt;
  
  
  If Nginx already redirects to HTTPS, do I still need SECURE_SSL_REDIRECT?
&lt;/h3&gt;

&lt;p&gt;Not necessarily. Warning W008 itself accepts redirecting at the load balancer or reverse proxy. If the proxy handles the redirect, silence that warning alone with SILENCED_SYSTEM_CHECKS = [“security.W008”].&lt;/p&gt;

&lt;h3&gt;
  
  
  Is there any risk in enabling HSTS?
&lt;/h3&gt;

&lt;p&gt;Browsers cache the HSTS policy for SECURE_HSTS_SECONDS; if HTTPS breaks, users cannot reach the site over HTTP until it expires. Start with a small value, for example 3600 seconds, and raise it gradually once the whole site runs stably on HTTPS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does check –deploy replace a security review?
&lt;/h3&gt;

&lt;p&gt;No. The command only scans your settings file — it does not inspect your code, third-party packages, or web server configuration. Treat it as the minimum check, not the whole security process.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Have you tried this in your own project?&lt;/strong&gt; If you hit an error or have a neater way to do it, leave a comment below — I read them all and will add the best ideas to the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Django documentation — Deployment checklist: &lt;a href="https://docs.djangoproject.com/en/6.1/howto/deployment/checklist/" rel="noopener noreferrer"&gt;https://docs.djangoproject.com/en/6.1/howto/deployment/checklist/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Django documentation — django-admin check: &lt;a href="https://docs.djangoproject.com/en/6.1/ref/django-admin/#check" rel="noopener noreferrer"&gt;https://docs.djangoproject.com/en/6.1/ref/django-admin/#check&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>FastAPI: Version Your API v1/v2 with APIRouter — Add a New Version Without Breaking Old Clients</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Tue, 24 Nov 2026 15:14:51 +0000</pubDate>
      <link>https://dev.to/linhfishdev/fastapi-version-your-api-v1v2-with-apirouter-add-a-new-version-without-breaking-old-clients-2978</link>
      <guid>https://dev.to/linhfishdev/fastapi-version-your-api-v1v2-with-apirouter-add-a-new-version-without-breaking-old-clients-2978</guid>
      <description>&lt;p&gt;Your API already has real clients using it, but you need to rename a field in the response — edit it directly and the old clients break immediately. The standard fix is versioning: split the routes into &lt;code&gt;/api/v1&lt;/code&gt; and &lt;code&gt;/api/v2&lt;/code&gt; with APIRouter, keep v1 exactly as it is, and put every breaking change into v2 only. This takes 5 minutes and applies whenever your API is public and you don’t control every client.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Suppose the /users endpoint returns {“id”: 1, “name”: “Linh”}. Now you want to split name into first_name and last_name, and add email. If you edit it directly, every old client reading the name field gets null or an error — a mobile app that hasn’t updated yet breaks. The problem is that a single URL serves both old and new clients, so every change is a breaking change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Linh"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;FastAPI solves this neatly with APIRouter: each version is its own router, mounted on the app with a different prefix. The business logic is written once in a shared module; the two routers differ only in how they present the response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# app/services/users.py — shared logic, written once&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"first_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Linh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"last_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hà"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"linh@example.com"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# app/api/v1/users.py — old shape, unchanged for old clients&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;APIRouter&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_users&lt;/span&gt;

&lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;APIRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'{u["first_name"]} {u["last_name"]}'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;get_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# app/api/v2/users.py — new shape&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;APIRouter&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_users&lt;/span&gt;

&lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;APIRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;get_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;v1&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;users_v1&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;v2&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;users_v2&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include_router&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users_v1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/api/v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"v1"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include_router&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users_v2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/api/v2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"v2"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;/api/v1/users&lt;/code&gt; still returns exactly the old shape, while &lt;code&gt;/api/v2/users&lt;/code&gt; returns the new one. Old clients change nothing, new clients use v2. The key point: a version only changes the API surface (field names, response shape), it doesn’t duplicate business logic — &lt;code&gt;get_users()&lt;/code&gt; exists in one copy, and both routers call it. When you need v3, just create a v3 router and one more &lt;code&gt;include_router&lt;/code&gt; line, and &lt;code&gt;tags=["v1"]&lt;/code&gt; groups endpoints by version in Swagger UI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Apply this rule to your project: once a version is released, never edit its response — only add new fields (backward compatible) or create a new version. Document which versions are still supported, and watch the logs to know when no requests hit v1 anymore before removing it. This also lets you deprecate gradually: announce v1’s end-of-support date instead of cutting it off suddenly and killing old clients.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/2026/10/05/fastapi-chia-api-thanh-nhieu-module-bang-apirouter-khoi-don-het-vao-main-py/" rel="noopener noreferrer"&gt;FastAPI: chia API thành nhiều module bằng APIRouter, khỏi dồn hết vào main.py&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/23/fastapi-redirect-after-post-with-redirectresponse-stop-f5-from-resubmitting-your-form-in-5-minutes/" rel="noopener noreferrer"&gt;FastAPI: Redirect After POST with RedirectResponse — Stop F5 From Resubmitting Your Form in 5 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/19/fastapi-customize-your-openapi-docs-change-the-title-tag-endpoints-and-hide-internal-routes-in-5-minutes/" rel="noopener noreferrer"&gt;FastAPI: Customize Your OpenAPI Docs — Change the Title, Tag Endpoints, and Hide Internal Routes in 5 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should the version go in the URL or in a header?
&lt;/h3&gt;

&lt;p&gt;In the URL (/api/v1/…) — it’s the most common approach: easy to test in a browser, easy to cache, easy to read in logs. A version in a header (Accept: application/vnd.api.v1+json) keeps the URL cleaner but is harder to try by hand and harder to debug. For most projects, the version in the URL is enough.&lt;/p&gt;

&lt;h3&gt;
  
  
  When do I need to create a new version?
&lt;/h3&gt;

&lt;p&gt;Only for a breaking change: renaming or deleting a field, changing a data type, changing what a field means, changing a status code. Adding a new field to the response or a new endpoint doesn’t need one — old clients safely ignore unfamiliar fields.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I have to duplicate all the code for each version?
&lt;/h3&gt;

&lt;p&gt;No. Only the thin route layer is duplicated: two routers call the same service, the same models, the same query functions. If v2 differs from v1 in only a few endpoints, rewrite just those endpoints and import the rest from v1.&lt;/p&gt;

&lt;h3&gt;
  
  
  How long should I keep v1 before removing it?
&lt;/h3&gt;

&lt;p&gt;Keep it until the logs show no client has called v1 for a long enough stretch (for example 3–6 months), and after you’ve announced the deprecation. Remove it too early and old clients die; keep it forever and it’s technical debt — watch the weekly v1 request count to decide.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Have you tried this in your own project?&lt;/strong&gt; If you hit an error or have a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fastapi.tiangolo.com/tutorial/bigger-applications/" rel="noopener noreferrer"&gt;FastAPI — Bigger Applications – Multiple Files&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fastapi.tiangolo.com/reference/api_router/" rel="noopener noreferrer"&gt;FastAPI — APIRouter reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design" rel="noopener noreferrer"&gt;Microsoft — API design best practices (versioning)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://linhfish.dev/en/2026/11/24/fastapi-version-your-api-v1-v2-with-apirouter-add-a-new-version-without-breaking-old-clients/" rel="noopener noreferrer"&gt;linhfish.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>api</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Google's site: search now comes back empty for pages it has indexed</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Mon, 23 Nov 2026 16:28:55 +0000</pubDate>
      <link>https://dev.to/linhfishdev/googles-site-search-now-comes-back-empty-for-pages-it-has-indexed-5bh2</link>
      <guid>https://dev.to/linhfishdev/googles-site-search-now-comes-back-empty-for-pages-it-has-indexed-5bh2</guid>
      <description></description>
      <category>seo</category>
      <category>google</category>
      <category>webdev</category>
      <category>automation</category>
    </item>
    <item>
      <title>JavaScript: Read and Build Query Strings Safely with URLSearchParams in 5 Minutes</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Mon, 23 Nov 2026 15:13:07 +0000</pubDate>
      <link>https://dev.to/linhfishdev/javascript-read-and-build-query-strings-safely-with-urlsearchparams-in-5-minutes-2oam</link>
      <guid>https://dev.to/linhfishdev/javascript-read-and-build-query-strings-safely-with-urlsearchparams-in-5-minutes-2oam</guid>
      <description>&lt;p&gt;A query string is the common way to pass filters, pagination or search keywords through a URL, but hand-built string concatenation goes wrong easily: forgetting to encode special characters, an &amp;amp; splitting a parameter in two, Vietnamese text turning into strange characters. URLSearchParams — built into browsers and Node.js — reads, adds, edits and deletes parameters with a few methods, encoding and decoding correctly by default. Use it whenever you need to read ?page=2 from a URL or build a URL for an API call with filters.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Take a product list page at /products?category=ao-thun&amp;amp;sort=price&amp;amp;page=2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`/api/products?category=${category}&amp;amp;sort=${sort}&amp;amp;page=${page}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three weaknesses: values with spaces or Vietnamese diacritics — for example “áo thun nam” — produce an invalid URL if you forget encodeURIComponent. A value containing &amp;amp; — for example “đồ ăn &amp;amp; uống” — gets cut into a new parameter, making the filter wrong. Reading it back with split(‘&amp;amp;’) then split(‘=’) breaks when a value contains = or an already-encoded character. Forget encodeURIComponent in just one place, and the bug is already silently there.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;URLSearchParams replaces manual string concatenation. To read parameters, pass window.location.search to the constructor, then use get(), getAll() and has():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// URL: /products?category=ao-thun&amp;amp;tag=sale&amp;amp;tag=new&amp;amp;page=2&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "ao-thun"&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// "2" — always a string&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tag"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// ["sale", "new"]&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"sort"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"missing"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: get() only returns the first value, so a repeated parameter like ?tag=sale&amp;amp;tag=new needs getAll(); a missing key returns null; every value is a string.&lt;/p&gt;

&lt;p&gt;To build a query string, use set() and append() on an empty object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"q"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"áo thun nam"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tag"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sale"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tag"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"new"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "q=%C3%A1o+thun+nam&amp;amp;page=2&amp;amp;tag=sale&amp;amp;tag=new"&lt;/span&gt;
&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`/api/products?${params}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;set() overwrites the old value of a key, append() adds a new value while keeping the old one. toString() encodes every special character correctly — spaces become +, accented letters become %XX — so it can go straight after the ?.&lt;/p&gt;

&lt;p&gt;url.searchParams is linked directly to the URL: set() or delete() updates url.search immediately; combine it with history.pushState() to change pages without reloading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"3"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"sort"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pushState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// URL changes, the page does not reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not pass a full URL into the constructor — it only takes a query string — and do not interpolate a string containing + into a query, because + becomes a space; values like that must go through append().&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Use URLSearchParams everywhere you build filter links, pagination and API calls with parameters: a search box that syncs its keyword to the URL so a shared link keeps the same results; a page button that only needs set(“page”, …) without losing the filters already selected; when the backend returns an error because of a malformed query, print params.toString() to the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/10/18/javascript-fetch-has-no-built-in-timeout-cancel-hanging-requests-with-abortcontroller-in-5-minutes/" rel="noopener noreferrer"&gt;JavaScript: fetch() Has No Built-in Timeout — Cancel Hanging Requests with AbortController in 5 Minutes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/08/javascript-run-multiple-promises-at-once-with-promise-allsettled-one-failure-wont-ruin-the-batch/" rel="noopener noreferrer"&gt;JavaScript: Run Multiple Promises at Once with Promise.allSettled — One Failure Won’t Ruin the Batch&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/18/javascript-stop-api-spam-with-debounce-the-search-box-only-calls-the-api-when-you-stop-typing/" rel="noopener noreferrer"&gt;JavaScript: Stop API Spam with Debounce — the Search Box Only Calls the API When You Stop Typing&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does URLSearchParams work in Node.js?
&lt;/h3&gt;

&lt;p&gt;Yes — it has been a global since Node.js 10, and works exactly like in the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between get() and getAll()?
&lt;/h3&gt;

&lt;p&gt;get() returns the first value, or null if the key does not exist. getAll() returns an array of all values; for a repeated parameter like ?tag=sale&amp;amp;tag=new, getAll() is required.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between set() and append()?
&lt;/h3&gt;

&lt;p&gt;set() overwrites: it deletes the old value and sets the new one. append() adds a new value while keeping the old one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does a + in a value turn into a space?
&lt;/h3&gt;

&lt;p&gt;Under the application/x-www-form-urlencoded standard, + means a space. To keep a literal +, pass the value through append() or set() so it becomes %2B.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Have you tried this in your own project?&lt;/strong&gt; If you hit an error or have a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;MDN Web Docs — URLSearchParams: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;WHATWG URL Standard — URLSearchParams: &lt;a href="https://url.spec.whatwg.org/#urlsearchparams" rel="noopener noreferrer"&gt;https://url.spec.whatwg.org/#urlsearchparams&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Flask: Separate Templates and Static Files per Blueprint — Stop Your templates/ Folder from Ballooning in 5 Minutes</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 15:13:37 +0000</pubDate>
      <link>https://dev.to/linhfishdev/flask-separate-templates-and-static-files-per-blueprint-stop-your-templates-folder-from-217i</link>
      <guid>https://dev.to/linhfishdev/flask-separate-templates-and-static-files-per-blueprint-stop-your-templates-folder-from-217i</guid>
      <description>&lt;p&gt;As a Flask app grows, templates and static files piling up in the root-level &lt;code&gt;templates/&lt;/code&gt; and &lt;code&gt;static/&lt;/code&gt; folders quickly turn into a mess: two modules can easily collide on &lt;code&gt;index.html&lt;/code&gt; or &lt;code&gt;style.css&lt;/code&gt;, and when you move a module to another project, its templates get left behind. The cleanest fix: pass &lt;code&gt;template_folder&lt;/code&gt; and &lt;code&gt;static_folder&lt;/code&gt; when you create a &lt;code&gt;Blueprint&lt;/code&gt;, so each module carries its own templates and static files.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Suppose your app has two blueprints — &lt;code&gt;admin&lt;/code&gt; and &lt;code&gt;shop&lt;/code&gt; — and both need an &lt;code&gt;index.html&lt;/code&gt; page. Without separate folders, both files have to live in the same &lt;code&gt;templates/&lt;/code&gt; directory, but only one file can have that name. The usual workaround is long names like &lt;code&gt;admin_index.html&lt;/code&gt; and &lt;code&gt;shop_index.html&lt;/code&gt;: the more modules you add, the longer the filenames get, and the harder it is to tell which file belongs to which module. Static files suffer the same problem: &lt;code&gt;static/style.css&lt;/code&gt; gives no clue whether it is the admin CSS or the shop CSS, and when you move the &lt;code&gt;admin&lt;/code&gt; module to another project, you have to hunt down each of its files scattered across the shared folders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app.py — mọi template dồn vào templates/ ở gốc&lt;/span&gt;
&lt;span class="n"&gt;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Blueprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"admin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;shop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Blueprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"shop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;admin_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"index.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# của admin hay shop?&lt;/span&gt;

&lt;span class="nd"&gt;@shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;shop_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"index.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# trùng tên — chỉ một file tồn tại&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Pass &lt;code&gt;template_folder&lt;/code&gt; and &lt;code&gt;static_folder&lt;/code&gt; when you create the blueprint, and put its templates in a subfolder named after the blueprint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# blueprints/admin/__init__.py&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Blueprint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;render_template&lt;/span&gt;

&lt;span class="n"&gt;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Blueprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"admin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;url_prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/admin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;template_folder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"templates"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;static_folder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"static"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"admin/index.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The directory structure now looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blueprints/
    admin/
        __init__.py
        templates/
            admin/
                index.html
        static/
            admin.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the template, point to the blueprint’s static files through the &lt;code&gt;admin.static&lt;/code&gt; endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jinja"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"{{ url_for('admin.static', filename='admin.css') }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A blueprint’s template folder is added to the search path with lower priority than the app’s &lt;code&gt;templates/&lt;/code&gt; folder, so the app can still override a blueprint’s template when needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Put templates in a subfolder named after the blueprint (&lt;code&gt;admin/index.html&lt;/code&gt;) to avoid accidental overrides: if two blueprints both have &lt;code&gt;index.html&lt;/code&gt; at the root of their template folder, the blueprint registered first wins.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;static_folder&lt;/code&gt; is only reachable when the blueprint has a &lt;code&gt;url_prefix&lt;/code&gt; — by default the file lives at &lt;code&gt;/admin/static/admin.css&lt;/code&gt;. Without a prefix, the app’s &lt;code&gt;/static&lt;/code&gt; route takes precedence and the blueprint’s static files are unreachable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;url_for("admin.static", filename="admin.css")&lt;/code&gt; generates the correct URL even if you change &lt;code&gt;url_prefix&lt;/code&gt; later, so don’t hardcode &lt;code&gt;/admin/static/...&lt;/code&gt; in your templates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When Flask loads the wrong template, set &lt;code&gt;app.config["EXPLAIN_TEMPLATE_LOADING"] = True&lt;/code&gt; and Flask will print each step of its search.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Combined with the Application Factory, each blueprint becomes a self-contained folder holding its routes, templates, and static files — copy the whole folder to another project and it just works. Keep in the root &lt;code&gt;templates/&lt;/code&gt; only what the whole app shares, like &lt;code&gt;base.html&lt;/code&gt;, macros, and error pages; split out only what belongs to a single module. A small module with just one or two templates doesn’t need its own folders — split when filenames start needing a module-name prefix just to avoid collisions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/10/01/flask-to-chuc-route-gon-gang-bang-blueprint-khi-du-an-lon-dan/" rel="noopener noreferrer"&gt;Flask: Organize Routes Neatly with Blueprints as Your Project Grows&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/09/30/flask-dung-application-factory-create_app-de-app-de-test-de-mo-rong-ngay-tu-dau/" rel="noopener noreferrer"&gt;Flask: Use the Application Factory (create_app) So Your App Is Easy to Test and Extend from Day One&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/11/flask-reuse-layouts-with-template-inheritance-extends-and-block-in-jinja2/" rel="noopener noreferrer"&gt;Flask: Reuse Layouts with Template Inheritance — extends and block in Jinja2&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Without &lt;code&gt;template_folder&lt;/code&gt;, where does a blueprint look for templates?
&lt;/h3&gt;

&lt;p&gt;Only in the app’s &lt;code&gt;templates/&lt;/code&gt; folder. Passing &lt;code&gt;template_folder&lt;/code&gt; adds the blueprint’s own folder to the search path, and the app’s folder still takes priority.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two blueprints both have &lt;code&gt;index.html&lt;/code&gt; — which file does Flask pick?
&lt;/h3&gt;

&lt;p&gt;The app’s template wins first; among blueprints, the one registered first wins. The safest approach: put templates in a subfolder named after the blueprint, for example &lt;code&gt;admin/index.html&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does &lt;code&gt;url_for('admin.static', ...)&lt;/code&gt; return 404 even though the file exists?
&lt;/h3&gt;

&lt;p&gt;Usually because the blueprint has no &lt;code&gt;url_prefix&lt;/code&gt;, so the app’s &lt;code&gt;/static&lt;/code&gt; route takes precedence. Add &lt;code&gt;url_prefix="/admin"&lt;/code&gt; (or &lt;code&gt;static_url_path&lt;/code&gt;) and check &lt;code&gt;/admin/static/&amp;lt;filename&amp;gt;&lt;/code&gt; again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I move all templates into blueprints?
&lt;/h3&gt;

&lt;p&gt;No. Shared templates like &lt;code&gt;base.html&lt;/code&gt;, macros, and error pages should stay in the root folder so every blueprint can inherit them; split out only templates that belong to a single module.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I know which file Flask is loading a template from?
&lt;/h3&gt;

&lt;p&gt;Set &lt;code&gt;app.config["EXPLAIN_TEMPLATE_LOADING"] = True&lt;/code&gt;: Flask prints each search step every time &lt;code&gt;render_template&lt;/code&gt; runs, which is very useful when a template is being overridden and you can’t tell why.&lt;/p&gt;

&lt;p&gt;💬 Have you tried this in your own project? If you hit an error or have a cleaner approach, leave a comment below — I read them all and will add the best ideas to the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Flask Documentation — &lt;a href="https://flask.palletsprojects.com/en/stable/blueprints/" rel="noopener noreferrer"&gt;Modular Applications with Blueprints&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>flask</category>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>FastAPI: Store Login Sessions with SessionMiddleware in 5 Minutes</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Sat, 21 Nov 2026 15:12:30 +0000</pubDate>
      <link>https://dev.to/linhfishdev/fastapi-store-login-sessions-with-sessionmiddleware-in-5-minutes-37ja</link>
      <guid>https://dev.to/linhfishdev/fastapi-store-login-sessions-with-sessionmiddleware-in-5-minutes-37ja</guid>
      <description>&lt;p&gt;Starlette’s SessionMiddleware stores login state in a signed browser cookie: after a successful login, the server only needs to read request.session to know who the user is — no database or Redis required. Attach the middleware once for the whole app, set secret_key from an environment variable, and use it for small apps, internal admin panels, or prototypes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Many FastAPI apps start by storing the logged-in user in a module-level global variable. That works on a dev machine but breaks as soon as the app runs for real: a global only remembers one user — the next login overwrites the previous one; restarting the server wipes all sessions; and with multiple workers, each worker holds a different copy, so the same cookie sometimes logs you in and sometimes kicks you out.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;responses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;current_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;  &lt;span class="c1"&gt;# remembers only 1 user, gone on restart&lt;/span&gt;
&lt;span class="no"&gt;USERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"linh"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"mat-khau-123"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/login"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;USERS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"username"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;current_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"username"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Dang nhap thanh cong"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Sai tai khoan hoac mat khau"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/me"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;me&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Chua dang nhap"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;SessionMiddleware solves this by pushing session state down into the browser’s own cookie. The cookie is signed with secret_key, so the client cannot tamper with its contents — change a single character and the signature no longer matches, and the server treats the request as not logged in. Attach the middleware once, and every endpoint after that can read request.session like a normal dict.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;responses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;starlette&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sessions&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;SessionMiddleware&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_middleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;SessionMiddleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;secret_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SECRET_KEY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"dev-only-change-me"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# 1 ngay&lt;/span&gt;
    &lt;span class="n"&gt;same_site&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"lax"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;https_only&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# production dat True&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The three endpoints for login, session check, and logout are only a few lines each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;USERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"linh"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"mat-khau-123"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/login"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;USERS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"username"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Sai tai khoan hoac mat khau"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"username"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Dang nhap thanh cong"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/me"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;me&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Chua dang nhap"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/logout"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Da dang xuat"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key point: request.session is a dict — assign request.session[“user”] in the login endpoint, read request.session.get(“user”) in the endpoints you want to protect, and call request.session.clear() on logout. max_age sets the cookie’s lifetime (one day here); same_site=”lax” stops the cookie from being sent along from other sites; https_only=True should only be enabled once the app runs over HTTPS — turning it on early on a dev machine stops the cookie from being sent and makes the session mysteriously “disappear”.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;This approach fits best for internal admin panels, small dashboards, or prototypes that need a quick login without setting up Redis or a session table in the database. When you ship to production, take SECRET_KEY from an environment variable and generate it with secrets.token_hex(32) — never hardcode a short string like the one in the example above. If you later need to log a user out of all devices, or store more than the cookie’s roughly 4KB limit, switch to server-side sessions (for example Redis) and keep only the session id in the cookie.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/10/26/fastapi-set-a-secure-login-cookie-with-response-set_cookie-in-5-minutes/" rel="noopener noreferrer"&gt;FastAPI: Set a Secure Login Cookie with Response.set_cookie in 5 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/2026/10/11/fastapi-cau-hinh-cors-dung-cach-bang-corsmiddleware-trong-5-phut/" rel="noopener noreferrer"&gt;FastAPI: cấu hình CORS đúng cách bằng CORSMiddleware trong 5 phút&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/10/28/fastapi-read-headers-and-cookies-with-header-and-cookie-in-5-minutes/" rel="noopener noreferrer"&gt;FastAPI: Read Headers and Cookies with Header() and Cookie() in 5 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How is SessionMiddleware different from setting a cookie yourself with Response.set_cookie?
&lt;/h3&gt;

&lt;p&gt;set_cookie only writes a raw value to the browser — the client can edit it freely and the server cannot tell. SessionMiddleware signs the whole cookie content with secret_key, so any tampering is detected, and it provides a ready-made dict API through request.session.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can a session cookie store a password or a sensitive token?
&lt;/h3&gt;

&lt;p&gt;You should not. The cookie is only signed, not encrypted — anyone holding the cookie can still read its contents. Store only a user id or username; keep passwords and sensitive tokens on the server.&lt;/p&gt;

&lt;h3&gt;
  
  
  How much data can a cookie session hold?
&lt;/h3&gt;

&lt;p&gt;About 4KB — the browser’s general cookie limit. Go over it and the browser refuses to store the cookie, silently breaking the session. So store only a few small fields like a user id, and keep large data in the database or Redis.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I log a user out of every device they are logged in on?
&lt;/h3&gt;

&lt;p&gt;With cookie sessions, you cannot — the server keeps no list of sessions, so there is nothing to revoke; the only option is to change secret_key, which logs out absolutely everyone. To revoke individual sessions, use server-side sessions.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Have you tried this in your own project?&lt;/strong&gt; If you hit an error or have a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Starlette documentation — Sessions: &lt;a href="https://www.starlette.io/middleware/#sessionmiddleware" rel="noopener noreferrer"&gt;https://www.starlette.io/middleware/#sessionmiddleware&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;FastAPI documentation — Middleware: &lt;a href="https://fastapi.tiangolo.com/advanced/middleware/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/advanced/middleware/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>FastAPI: Validate Query Parameters with Query() — Reject Bad Values at the Gate</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Fri, 20 Nov 2026 15:12:45 +0000</pubDate>
      <link>https://dev.to/linhfishdev/fastapi-validate-query-parameters-with-query-reject-bad-values-at-the-gate-4okp</link>
      <guid>https://dev.to/linhfishdev/fastapi-validate-query-parameters-with-query-reject-bad-values-at-the-gate-4okp</guid>
      <description>&lt;p&gt;Unvalidated query parameters are a silent API bug: a client sends &lt;code&gt;page=0&lt;/code&gt; or &lt;code&gt;size=10000&lt;/code&gt;, the server still answers 200, then quietly returns wrong data or “carries” the whole table. FastAPI solves this with &lt;code&gt;Query()&lt;/code&gt; — declare the limits right in the function signature, and the framework checks them and returns a 422 error when a value is wrong, without a single extra &lt;code&gt;if&lt;/code&gt;. Use it for every endpoint that takes pagination, search, or filters.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;A product-listing endpoint with pagination and name search, written without &lt;code&gt;Query()&lt;/code&gt;, usually looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;ITEMS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"san-pham-{i}"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;51&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/items/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;ITEMS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, &lt;code&gt;page=0&lt;/code&gt; makes &lt;code&gt;start = -10&lt;/code&gt;, and the Python slice &lt;code&gt;[-10:0]&lt;/code&gt; returns the last 10 rows of the table — wrong data, but the status is still 200, so the client never knows it got the wrong page. Second, &lt;code&gt;size=10000&lt;/code&gt; forces the server to return the whole table — slow and memory-hungry, and a few requests like that are enough to choke the API. Third, a one-character &lt;code&gt;q&lt;/code&gt; still triggers a full-table search whose results are almost meaningless. Patching it with &lt;code&gt;if&lt;/code&gt; checks and &lt;code&gt;raise HTTPException&lt;/code&gt; at the top of every endpoint just repeats the same code everywhere, and one forgotten spot leaves a hole.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Instead of checking by hand, declare the limits right in the function signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;ITEMS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"san-pham-{i}"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;51&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/items/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ge&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ge&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;le&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ITEMS&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now a &lt;code&gt;page&lt;/code&gt; below 1, a &lt;code&gt;size&lt;/code&gt; above 100, or a &lt;code&gt;q&lt;/code&gt; shorter than 2 characters is rejected before the function even runs, and FastAPI returns a 422 with a clear message, for example &lt;code&gt;Input should be greater than or equal to 1&lt;/code&gt;. The four most-used options: &lt;code&gt;ge&lt;/code&gt;/&lt;code&gt;le&lt;/code&gt; bound numbers, &lt;code&gt;min_length&lt;/code&gt;/&lt;code&gt;max_length&lt;/code&gt; bound strings, &lt;code&gt;default&lt;/code&gt; sets the value used when the client sends nothing, and &lt;code&gt;pattern&lt;/code&gt; matches a regex when needed — for example a product code made of letters and digits only. Note that &lt;code&gt;ge&lt;/code&gt; allows exactly the limit while &lt;code&gt;gt&lt;/code&gt; does not — so &lt;code&gt;page&lt;/code&gt; uses &lt;code&gt;ge=1&lt;/code&gt;, not &lt;code&gt;gt=0&lt;/code&gt;, because &lt;code&gt;ge&lt;/code&gt; states the intent more clearly. If you already have a shared pagination dependency, attach &lt;code&gt;Query()&lt;/code&gt; to it once and every endpoint benefits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Apply this to every endpoint that takes data from the query string: list pages, search boxes, filters by status or date range. Always set &lt;code&gt;le&lt;/code&gt; on &lt;code&gt;size&lt;/code&gt; (100 is a good default) so nobody can “ask for” the whole table in one request, and set &lt;code&gt;min_length&lt;/code&gt; on search keywords to avoid meaningless queries scanning the entire table. The stricter the validation at the gate, the simpler the code inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/09/30/fastapi-xu-ly-viec-nang-o-background-voi-backgroundtasks-client-khong-phai-cho/" rel="noopener noreferrer"&gt;FastAPI: Handle Heavy Work in the Background with BackgroundTasks — the Client Never Waits&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/09/30/fastapi-quan-ly-db-session-an-toan-bang-dependency-yield-trong-5-phut/" rel="noopener noreferrer"&gt;FastAPI: Manage DB Sessions Safely with a Yield Dependency in 5 Minutes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/10/02/fastapi-dung-chung-dependency-phan-trang-cho-moi-endpoint-khoi-lap-lai-skip-limit/" rel="noopener noreferrer"&gt;FastAPI: Share One Pagination Dependency Across Every Endpoint — Stop Repeating skip/limit&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How is Query() different from Pydantic’s Field()?
&lt;/h3&gt;

&lt;p&gt;They share the same constraint system — &lt;code&gt;ge&lt;/code&gt;, &lt;code&gt;le&lt;/code&gt;, &lt;code&gt;min_length&lt;/code&gt; — but &lt;code&gt;Query()&lt;/code&gt; is for query parameters in the function signature, while &lt;code&gt;Field()&lt;/code&gt; is for fields inside a request-body model.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I force the client to send a parameter?
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;Query(...)&lt;/code&gt; with an ellipsis instead of a default value — if the parameter is missing, FastAPI returns a 422 right away.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I accept several values for one parameter, for example ?tag=a&amp;amp;tag=b?
&lt;/h3&gt;

&lt;p&gt;Declare &lt;code&gt;tags: list[str] = Query(default=[])&lt;/code&gt; and FastAPI collects the repeated values into a list automatically.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does the 422 error look like, and can I customize it?
&lt;/h3&gt;

&lt;p&gt;FastAPI returns JSON with a &lt;code&gt;detail&lt;/code&gt; array, each item naming the error location (&lt;code&gt;loc&lt;/code&gt;), the message (&lt;code&gt;msg&lt;/code&gt;), and the error type (&lt;code&gt;type&lt;/code&gt;). To change the format API-wide, register one exception handler for &lt;code&gt;RequestValidationError&lt;/code&gt; on the app.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Have you tried this approach in your own project?&lt;/strong&gt; If you ran into an error or know a more concise way to do it, leave a comment below — I read every one and will add the best ideas to the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;FastAPI docs — Query Parameters and String Validations: &lt;a href="https://fastapi.tiangolo.com/tutorial/query-params-str-validations/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/tutorial/query-params-str-validations/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;FastAPI docs — Query Parameters: &lt;a href="https://fastapi.tiangolo.com/tutorial/query-params/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/tutorial/query-params/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>webdev</category>
      <category>api</category>
    </item>
    <item>
      <title>FastAPI: Customize Your OpenAPI Docs — Change the Title, Tag Endpoints, and Hide Internal Routes in 5 Minutes</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Thu, 19 Nov 2026 15:13:08 +0000</pubDate>
      <link>https://dev.to/linhfishdev/fastapi-customize-your-openapi-docs-change-the-title-tag-endpoints-and-hide-internal-routes-in-1hd9</link>
      <guid>https://dev.to/linhfishdev/fastapi-customize-your-openapi-docs-change-the-title-tag-endpoints-and-hide-internal-routes-in-1hd9</guid>
      <description>&lt;p&gt;FastAPI generates Swagger UI and ReDoc documentation from your code automatically, but by default it carries the generic name “FastAPI” and lists every endpoint — including internal ones you would rather not show. Just pass title, description, version, and openapi_tags to FastAPI(), add tags to each route, and set include_in_schema=False on the endpoints you want to hide, and the docs become tidy and on-brand right away. This works for any FastAPI project, with no extra library to install.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;When you create an app with a bare FastAPI(), the /docs page shows the title “FastAPI”, a default description, and version “0.1.0”. Every endpoint lands in one “default” group with no description, so readers have to guess what each route does. Worse, internal endpoints such as /admin/stats or /internal/health also show up in the public docs — anyone who opens /docs can see how the system works. Writing documentation by hand in a separate file takes effort and drifts out of sync with the code after a few edits.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Pass metadata when you create the app, tag each route, and hide internal endpoints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Shop API"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Order management API for a demo store."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"1.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;openapi_tags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"orders"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Create and look up orders."&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"admin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Internal endpoints, for operations only."&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/orders"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"orders"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"total"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;250000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/admin/stats"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"admin"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include_in_schema&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;admin_stats&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"orders_today"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first three parameters change the “face” of the docs on the /docs page. openapi_tags declares the groups up front with descriptions, and tags=[“orders”] on the decorator puts each endpoint in the right group. include_in_schema=False only hides the endpoint from the schema — the route still works normally, it just does not appear in the docs.&lt;/p&gt;

&lt;p&gt;When you need more — a company logo, real server URLs — override app.openapi with your own function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utils&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_openapi&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;custom_openapi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openapi_schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openapi_schema&lt;/span&gt;
    &lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_openapi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"info"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"x-logo"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://example.com/logo.png"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"servers"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://api.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Production"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openapi_schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;schema&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openapi_schema&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openapi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;custom_openapi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;get_openapi() generates the standard schema just like the default — you only add the fields you need, then cache the result in app.openapi_schema so it is not regenerated on every request.&lt;/p&gt;

&lt;p&gt;In production, turn off Swagger UI and ReDoc entirely with docs_url=None and redoc_url=None:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;docs_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/docs"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ENV"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"prod"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;redoc_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to lock the schema file itself too, also set openapi_url=None. The API still works normally — there is just no public door left to view the docs through.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Apply this to your running API right now: set the title to the product name, keep the version in sync with your release version so readers know which version of the API they are reading about, and review your internal endpoints — health checks, admin stats, webhooks from third parties — and add include_in_schema=False to them. From then on, whenever you add a new endpoint, tag it right when you write the decorator, and the docs will stay tidy without periodic cleanups.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/10/12/fastapi-viet-middleware-tuy-chinh-do-thoi-gian-xu-ly-request-va-ghi-log-trong-5-phut/" rel="noopener noreferrer"&gt;FastAPI: Write Custom Middleware to Measure Request Time and Log in 5 Minutes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/10/11/fastapi-cau-hinh-cors-dung-cach-bang-corsmiddleware-trong-5-phut/" rel="noopener noreferrer"&gt;FastAPI: Configure CORS Correctly with CORSMiddleware in 5 Minutes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/2026/10/10/fastapi-chan-ro-ri-password-trong-response-bang-response_model-trong-5-phut/" rel="noopener noreferrer"&gt;FastAPI: Stop Password Leaks in Responses with response_model in 5 Minutes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does include_in_schema=False also turn off the endpoint?
&lt;/h3&gt;

&lt;p&gt;No. The endpoint still accepts requests and returns responses normally — it just disappears from /openapi.json and from the /docs page. This is a way to hide documentation, not a security measure — sensitive endpoints still need authentication as usual.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does changing the title and version affect clients generated from OpenAPI?
&lt;/h3&gt;

&lt;p&gt;Yes, but for the better. Client generators read the title and version from the schema, so a proper name and version help the generated client get the right package name and API version. Keep the version in sync with your real release version.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I move /docs to a different path?
&lt;/h3&gt;

&lt;p&gt;Yes. Pass docs_url=”/documentation” when you create the app and Swagger UI moves to /documentation. Similarly, redoc_url changes the ReDoc path and openapi_url changes the schema JSON path.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does overriding app.openapi mean rewriting the whole schema?
&lt;/h3&gt;

&lt;p&gt;No. The get_openapi() function generates the full schema from your routes — you only add or edit a few fields and return it. Remember to cache the result in app.openapi_schema so each request to /openapi.json does not regenerate the schema from scratch.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Have you tried this in your own project?&lt;/strong&gt; If you hit an error or have a cleaner approach, leave a comment below — I read them all and will add the best ideas to the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;FastAPI — Extending OpenAPI: &lt;a href="https://fastapi.tiangolo.com/how-to/extending-openapi/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/how-to/extending-openapi/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;FastAPI — Metadata and Docs URLs: &lt;a href="https://fastapi.tiangolo.com/tutorial/metadata/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/tutorial/metadata/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>openapi</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Django: Block GET Requests from Deleting Data with @require_POST in 5 Minutes</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Wed, 18 Nov 2026 15:14:49 +0000</pubDate>
      <link>https://dev.to/linhfishdev/django-block-get-requests-from-deleting-data-with-requirepost-in-5-minutes-2hed</link>
      <guid>https://dev.to/linhfishdev/django-block-get-requests-from-deleting-data-with-requirepost-in-5-minutes-2hed</guid>
      <description>&lt;p&gt;A view that deletes data and accepts GET is a silent security bug: any link, crawler, or image tag can trigger a delete without anyone clicking a button. The neatest fix is to put the &lt;code&gt;@require_POST&lt;/code&gt; decorator from &lt;code&gt;django.views.decorators.http&lt;/code&gt; on the view — every non-POST request gets a 405 Method Not Allowed. This rule applies to every view that changes data: deleting a post, approving a comment, changing an order status, or activating an account.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Suppose you have a view that deletes a post like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# views.py — DANGEROUS: deleting a post with a GET link&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shortcuts&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_object_or_404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;redirect&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;delete_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_object_or_404&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"post_list"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# urls.py&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"posts/&amp;lt;int:post_id&amp;gt;/delete/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delete_post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"delete_post"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The delete URL is now just an ordinary GET link. Crawlers, link preview tools, or a malicious &lt;code&gt;img&lt;/code&gt; tag pointing at this URL all send GET requests — and the post disappears without anyone noticing. The browser doesn’t ask first either: the CSRF token only protects “unsafe” requests like POST, not GET.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Adding two decorators makes the delete view safe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# views.py — only POST may delete&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contrib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decorators&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;login_required&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shortcuts&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_object_or_404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;redirect&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decorators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;require_POST&lt;/span&gt;

&lt;span class="nd"&gt;@login_required&lt;/span&gt;
&lt;span class="nd"&gt;@require_POST&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;delete_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_object_or_404&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"post_list"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;!--&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="n"&gt;must&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"post"&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{% url 'delete_post' post.id %}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;csrf_token&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"submit"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Delete&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now a GET request gets 405 Method Not Allowed with an &lt;code&gt;Allow: POST&lt;/code&gt; header, while a valid POST still works normally. Two details worth remembering: put &lt;code&gt;@login_required&lt;/code&gt; above &lt;code&gt;@require_POST&lt;/code&gt; so an unauthenticated request is redirected to the login page first instead of getting a 405; and &lt;code&gt;@require_POST&lt;/code&gt; doesn’t replace the CSRF token — the POST form still needs &lt;code&gt;{% csrf_token %}&lt;/code&gt; to block forged requests from other sites. It also doesn’t replace the ownership check: filter &lt;code&gt;author=request.user&lt;/code&gt; so a user can only delete their own posts.&lt;/p&gt;

&lt;p&gt;Three sibling decorators in the same module cover the remaining cases: &lt;code&gt;@require_http_methods(["GET", "POST"])&lt;/code&gt; for a view that both shows a form and handles its submit; &lt;code&gt;@require_safe&lt;/code&gt; for read-only views, accepting both GET and HEAD; class-based views use &lt;code&gt;method_decorator(require_POST, name="dispatch")&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Open your project and list every view that changes data: delete, change status, send email, activate an account. Put &lt;code&gt;@require_POST&lt;/code&gt; on each one, turn the links that call them into POST forms with &lt;code&gt;{% csrf_token %}&lt;/code&gt;, then test with &lt;code&gt;curl -X GET&lt;/code&gt; — the server must return 405 instead of performing the action. One afternoon of review, and a whole class of “delete by link” holes is gone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/10/29/django-fetch-objects-safely-with-get_object_or_404-skip-try-except-doesnotexist-in-5-minutes/" rel="noopener noreferrer"&gt;Django: Fetch Objects Safely with get_object_or_404 — Skip try/except DoesNotExist in 5 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/11/django-protect-views-for-logged-in-users-only-with-login_required-and-loginrequiredmixin-in-5-minutes/" rel="noopener noreferrer"&gt;Django: Protect Views for Logged-in Users Only with @login_required and LoginRequiredMixin in 5 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/15/django-test-views-with-testcase-and-client-catch-bugs-before-deploy-in-5-minutes/" rel="noopener noreferrer"&gt;Django: Test Views with TestCase and Client — Catch Bugs Before Deploy in 5 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does @require_POST replace the CSRF token?
&lt;/h3&gt;

&lt;p&gt;No. &lt;code&gt;@require_POST&lt;/code&gt; only blocks the wrong method; a forged POST from another site still needs the CSRF token to be blocked. The two layers complement each other; neither replaces the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does the server return for a wrong-method request?
&lt;/h3&gt;

&lt;p&gt;A 405 Method Not Allowed error, with an &lt;code&gt;Allow&lt;/code&gt; header listing the permitted methods — for example &lt;code&gt;Allow: POST&lt;/code&gt;. The client immediately understands this is a wrong-method error, not a server error.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I use it with a class-based view?
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;method_decorator(require_POST, name="dispatch")&lt;/code&gt; on the class to apply it to the whole view. Putting &lt;code&gt;@require_POST&lt;/code&gt; directly on the &lt;code&gt;post()&lt;/code&gt; method works too, but &lt;code&gt;dispatch&lt;/code&gt; covers the other methods as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about a view that both shows a form and handles its submit?
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;@require_http_methods(["GET", "POST"])&lt;/code&gt;. GET shows the form, POST processes the data, and PUT or DELETE both get a 405.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do @require_GET and @require_safe differ?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;@require_GET&lt;/code&gt; only allows GET, while &lt;code&gt;@require_safe&lt;/code&gt; allows both GET and HEAD. Use &lt;code&gt;@require_safe&lt;/code&gt; for read-only views, because many link-checking tools send HEAD requests to probe.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Have you tried this in your own project?&lt;/strong&gt; If you hit an error or have a shorter way, leave a comment below — I read them all and will fold the good ideas into the post.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.djangoproject.com/en/5.2/topics/http/decorators/" rel="noopener noreferrer"&gt;Django documentation — View decorators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.djangoproject.com/en/5.2/ref/csrf/" rel="noopener noreferrer"&gt;Django documentation — Cross Site Request Forgery protection&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>security</category>
    </item>
    <item>
      <title>Django in Production #20: Series Wrap-Up — Operations Runbook, Incident Checklist, and What's Next</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Tue, 17 Nov 2026 15:12:56 +0000</pubDate>
      <link>https://dev.to/linhfishdev/django-in-production-20-series-wrap-up-operations-runbook-incident-checklist-and-whats-next-ld</link>
      <guid>https://dev.to/linhfishdev/django-in-production-20-series-wrap-up-operations-runbook-incident-checklist-and-whats-next-ld</guid>
      <description>&lt;p&gt;This final episode wraps up the journey of taking the expense tracker to production: by the end, you will have a complete operations runbook, an incident checklist you can use on your very next on-call shift, and a clear upgrade path. Following episode 19 on scaling with more workers and a read replica, this episode pulls the whole series together into a sustainable operations routine.&lt;/p&gt;

&lt;h2&gt;
  
  
  What an Operations Runbook Is and What It Contains
&lt;/h2&gt;

&lt;p&gt;A runbook is a living document that records every operations task: who does what, in which order, when deploying, when something breaks, and when you need to scale. A good runbook has four parts: the standard deploy procedure, how monitoring and alerting work, the backup and restore procedure, and an incident checklist by severity. Keep the runbook in the repo and review it every quarter, or after every major incident.&lt;/p&gt;

&lt;h2&gt;
  
  
  Incident Checklist by Severity
&lt;/h2&gt;

&lt;p&gt;When an incident happens, the first job is not to fix code — it is to determine the severity. A P1 incident means the site is completely down: check the &lt;code&gt;/health&lt;/code&gt; endpoint, see whether Gunicorn is still running, check the PostgreSQL connection, and if a deploy just finished, roll back immediately instead of debugging on production. A P2 incident means the system is slow: look at the slow query log, check the Redis hit rate, and see whether the Celery queue is backed up. A P3 incident is an isolated error: read the logs in Sentry and work out whether the error repeats in a pattern. Record every incident with its time, symptoms, cause, and fix, so next time you only need to open the record.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# scripts/incident-triage.sh — chạy khi site có dấu hiệu bất thường&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -e
curl -fsS https://example.com/health/ &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"ALERT: health endpoint khong phan hoi"&lt;/span&gt;
pg_isready -h db.example.com -p 5432 &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"ALERT: PostgreSQL khong san sang"&lt;/span&gt;
redis-cli -h cache.example.com ping &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"ALERT: Redis khong phan hoi"&lt;/span&gt;
systemctl is-active gunicorn celery &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"ALERT: service dung"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What’s Next on the Upgrade Path
&lt;/h2&gt;

&lt;p&gt;Once the runbook is stable, the three most worthwhile investments are Kubernetes, observability, and deeper automation. Kubernetes replaces manual deploys when you have many services, need pods to recover on their own, and want to scale with load. Observability takes the monitoring from episode 8 to the next level: distributed tracing with OpenTelemetry, structured logs, and one unified dashboard for Django, Celery, and Nginx. Finally, automate the runbook itself: turn the manual checklist into a check script, then into automated alerts. Only take each upgrade step when there is a real need for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# core/management/commands/ops_status.py&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;management&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseCommand&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;connections&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseCommand&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;help&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Kiem tra nhanh DB va cache truoc khi deploy"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;connections&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SUCCESS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PostgreSQL: OK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ops_probe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ops_probe"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SUCCESS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Redis cache: OK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;Spend this afternoon writing your first runbook for the expense tracker: start from your current deploy procedure, write down every command you now run by hand, then add the P1, P2, and P3 checklists using the template above. A good production system is not one that never fails — it is one where every failure is handled faster than the last.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/15/django-in-production-19-scaling-more-workers-load-balancing-read-replicas-and-when-you-actually-need-to-scale/" rel="noopener noreferrer"&gt;Django in Production #19: Scaling — More Workers, Load Balancing, Read Replicas, and When You Actually Need to Scale&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/14/django-in-production-18-zero-downtime-deploys-blue-green-rolling-migration-order-and-restarts/" rel="noopener noreferrer"&gt;Django in Production #18: Zero-Downtime Deploys — Blue-Green/Rolling, Migration Order and Restarts&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://linhfish.dev/en/2026/11/13/django-in-production-17-ci-cd-with-github-actions-automated-tests-then-deploy/" rel="noopener noreferrer"&gt;Django in Production #17: CI/CD with GitHub Actions — Automated Tests, Then Deploy&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How is a runbook different from system documentation?
&lt;/h3&gt;

&lt;p&gt;A runbook focuses on what to do when deploying or when something breaks, while system documentation describes the architecture and the reasons behind design decisions. When you are on call at 2 a.m. during an incident, you need the runbook, not an architecture diagram.&lt;/p&gt;

&lt;h3&gt;
  
  
  When should you move to Kubernetes?
&lt;/h3&gt;

&lt;p&gt;When you have many services to manage together, need pods to recover on their own when they fail, or need to scale with load in ways systemd and manual deploys cannot handle. For a single Django app, Gunicorn plus systemd is still good enough.&lt;/p&gt;

&lt;h3&gt;
  
  
  How short should an incident checklist be?
&lt;/h3&gt;

&lt;p&gt;Short enough to read through in five minutes under stress — usually five to seven steps per severity level. Put the longer detail in the explanation below each step.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/stable/howto/deployment/checklist/" rel="noopener noreferrer"&gt;Django Documentation — Deployment checklist&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/stable/topics/logging/" rel="noopener noreferrer"&gt;Django Documentation — Logging&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>devops</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Django in Production #19: Scaling — More Workers, Load Balancing, Read Replicas, and When You Actually Need to Scale</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Mon, 16 Nov 2026 15:13:22 +0000</pubDate>
      <link>https://dev.to/linhfishdev/django-in-production-19-scaling-more-workers-load-balancing-read-replicas-and-when-you-1mp9</link>
      <guid>https://dev.to/linhfishdev/django-in-production-19-scaling-more-workers-load-balancing-read-replicas-and-when-you-1mp9</guid>
      <description>&lt;p&gt;Episode 18 showed you how to deploy with zero downtime, but a single server running 3 Gunicorn workers will hit its limit sooner or later: CPU saturates, requests queue up, and the statistics page gets slower and slower. This episode shows how to scale one step at a time — add workers, balance the load with Nginx, split off a read replica for PostgreSQL — and the signs that tell you when scaling is actually needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Add Workers Before Adding Machines
&lt;/h2&gt;

&lt;p&gt;Before buying another server, squeeze everything out of the one you have. The classic Gunicorn formula is two times the CPU count plus one, but that is only a starting point — measure the real CPU usage and response latency. If CPU still has headroom but requests are slow, the problem is usually a bad query or missing cache — go back to episode 12 and episode 9 before scaling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check the real load before deciding to scale&lt;/span&gt;
uptime
curl -s http://localhost:8000/health/ -w &lt;span class="s2"&gt;"\n%{time_total}s\n"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Load Balancing with Nginx
&lt;/h2&gt;

&lt;p&gt;When one machine is no longer enough, run the app on two machines and let Nginx spread requests across them. Nginx has been the reverse proxy since episode 3 — now you only add servers to the upstream and pick a load distribution algorithm. &lt;code&gt;least_conn&lt;/code&gt; sends each request to the machine with the fewest active connections, which suits heavy requests such as monthly statistics. One important note: the app must be stateless — sessions and file uploads must not live on local disk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;upstream&lt;/span&gt; &lt;span class="n"&gt;expense_app&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;least_conn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.11&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="n"&gt;chi&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;tieu&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;proxy_pass&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//expense_app;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="nc"&gt;Host&lt;/span&gt; $&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: A Read Replica for PostgreSQL
&lt;/h2&gt;

&lt;p&gt;Most apps read far more than they write: transaction lists, monthly statistics, and reports are all reads. Split off a read replica to carry the read queries, while the primary database only handles writes. Django supports multiple databases out of the box: declare an extra &lt;code&gt;replica&lt;/code&gt; in &lt;code&gt;DATABASES&lt;/code&gt;, then use a small router to send reads to the replica while writes still go to the primary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# settings.py&lt;/span&gt;
&lt;span class="no"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"ENGINE"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"django.db.backends.postgresql"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"NAME"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"expense"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"HOST"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"db-primary.internal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"replica"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"ENGINE"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"django.db.backends.postgresql"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"NAME"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"expense"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"HOST"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"db-replica.internal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;DATABASE_ROUTERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"expense.routers.ReadReplicaRouter"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# expense/routers.py&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReadReplicaRouter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;db_for_read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hints&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"replica"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;db_for_write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hints&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"default"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;allow_migrate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;app_label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hints&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"default"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When Do You Actually Need to Scale?
&lt;/h2&gt;

&lt;p&gt;Do not scale on a feeling. Scale when at least one clear signal persists for several days: CPU above 70% during peak hours, p95 latency double the normal level, or the database connection pool running dry even with PgBouncer from episode 4. Conversely, if errors only appear in one short window, treat it as unusual traffic or a single slow query. Every scaling step adds one more thing to monitor — only expand when the numbers from episode 8 prove you have hit the ceiling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;For the expense tracker app, start by measuring: record CPU, p95 latency, and database connections for one week. If CPU stays under 50%, do nothing yet — optimize slow queries first. When you need more, add workers, then move to two-machine load balancing; only split off a read replica when read queries are actually hammering the primary database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/14/django-in-production-18-zero-downtime-deploys-blue-green-rolling-migration-order-and-restarts/" rel="noopener noreferrer"&gt;Django in Production #18: Zero-Downtime Deploys — Blue-Green/Rolling, Migration Order and Restarts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/10/31/django-production-operations-4-postgresql-in-production-connections-conn_max_age-pgbouncer-pooling-basic-backups/" rel="noopener noreferrer"&gt;Django Production Operations #4: PostgreSQL in Production — Connections, CONN_MAX_AGE, PgBouncer Pooling, Basic Backups&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/05/django-running-in-production-9-caching-with-redis-cache-framework-invalidation-patterns-and-avoiding-stale-data/" rel="noopener noreferrer"&gt;Django Running in Production #9: Caching with Redis — Cache Framework, Invalidation Patterns, and Avoiding Stale Data&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How many Gunicorn workers are enough?
&lt;/h3&gt;

&lt;p&gt;Start with two times the CPU count plus one, then measure the real numbers. Adding workers only helps when CPU still has headroom; if the database or a query is the bottleneck, more workers only make everything slower.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does a read replica serve stale data?
&lt;/h3&gt;

&lt;p&gt;Yes — replicas lag by tens of milliseconds to a few seconds. Never read freshly written data from a replica: after creating a new transaction, read it back from the primary database, or use &lt;code&gt;using("default")&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does load balancing need sticky sessions?
&lt;/h3&gt;

&lt;p&gt;No, as long as the app is stateless: sessions stored in Redis or the database, files on S3. Sticky sessions are only a temporary patch when the app still keeps state on local disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.djangoproject.com/en/stable/topics/db/multi-db/" rel="noopener noreferrer"&gt;Django docs — Multiple databases&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nginx.org/en/docs/http/load_balancing.html" rel="noopener noreferrer"&gt;Nginx — Using nginx as HTTP load balancer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gunicorn.org/en/stable/design.html" rel="noopener noreferrer"&gt;Gunicorn docs — design and worker processes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://linhfish.dev/en/2026/11/15/django-in-production-19-scaling-more-workers-load-balancing-read-replicas-and-when-you-actually-need-to-scale/" rel="noopener noreferrer"&gt;linhfish.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>devops</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Django in Production #18: Zero-Downtime Deploys — Blue-Green/Rolling, Migration Order and Restarts</title>
      <dc:creator>Linh Fish Dev</dc:creator>
      <pubDate>Sun, 15 Nov 2026 15:12:48 +0000</pubDate>
      <link>https://dev.to/linhfishdev/django-in-production-18-zero-downtime-deploys-blue-greenrolling-migration-order-and-restarts-k13</link>
      <guid>https://dev.to/linhfishdev/django-in-production-18-zero-downtime-deploys-blue-greenrolling-migration-order-and-restarts-k13</guid>
      <description>&lt;p&gt;Episode 17 built the CI/CD pipeline: push code to main, tests run, then the deploy goes to the server automatically. But an automatic deploy isn’t necessarily a zero-downtime one — restart the app while a user is entering an expense and their request fails with a 502. This episode finishes the last step: shipping the new version without users noticing, and rolling back in seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Stop-and-Restart Deploys Cause Downtime
&lt;/h2&gt;

&lt;p&gt;The simplest deploy is: stop the service, pull the new code, run migrations, then start again. In the gap between stopping and the new app being ready, every request fails — with Gunicorn, usually 5–30 seconds. A zero-downtime deploy removes that gap by always keeping at least one version of the app running.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Golden Rule: Migrations First, Code Second
&lt;/h2&gt;

&lt;p&gt;The correct order for every deploy is: pull the new code, run migrations, then restart the app. Migrations must be backward compatible — old code still runs on the new schema — following the episode 6 pattern: add a nullable column first, deploy the new code, then clean up the old column next time. Do it the other way around and the new app will crash on the old schema immediately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -e
&lt;span class="nb"&gt;cd&lt;/span&gt; ~/expense-app
git pull origin main
python manage.py migrate --noinput
python manage.py collectstatic --noinput
sudo systemctl reload gunicorn
curl -fsS http://localhost:8000/health/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rolling Deploys with Gunicorn and systemd
&lt;/h2&gt;

&lt;p&gt;A rolling deploy is the simplest approach: replace workers one at a time instead of stopping the whole app. Gunicorn supports graceful reload — send the HUP signal, the master process starts new workers running the new code, while old workers finish their in-flight requests before shutting down. The systemd config below uses exactly this mechanism, so a deploy is just one reload command — good enough for a single-server expense tracker.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight systemd"&gt;&lt;code&gt;&lt;span class="k"&gt;[Service]&lt;/span&gt;
&lt;span class="na"&gt;ExecStart&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;linh&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;venv&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;gunicorn&lt;/span&gt; &lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wsgi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="mf"&gt;127.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;workers&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="na"&gt;ExecReload&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;kill&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="no"&gt;HUP&lt;/span&gt; $&lt;span class="no"&gt;MAINPID&lt;/span&gt;
&lt;span class="na"&gt;KillMode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mixed&lt;/span&gt;
&lt;span class="na"&gt;TimeoutStopSec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Blue-Green Deploys with Nginx
&lt;/h2&gt;

&lt;p&gt;When you need more certainty — for example a big change, or an instant rollback — use blue-green: run two versions of the app side by side on two ports, with Nginx pointing only at the live one. Deploy the new version to the green port, verify it, then switch the upstream to green; the old version keeps running untouched, so rollback is just switching the upstream back. It costs double the resources, but in return you get a rewind button that works in seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;upstream&lt;/span&gt; &lt;span class="n"&gt;expense_app&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="mf"&gt;127.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8001&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;# blue: ban dang chay&lt;/span&gt;
    &lt;span class="c1"&gt;# server 127.0.0.1:8002;  # green: ban moi, bat len khi deploy
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="n"&gt;chi&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;tieu&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;proxy_pass&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//expense_app;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="nc"&gt;Host&lt;/span&gt; $&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Health Checks and Rollback
&lt;/h2&gt;

&lt;p&gt;Rolling or blue-green, never send traffic to the new version before confirming it’s alive. After the reload or upstream switch, call the /health/ endpoint from episode 8 and wait until it returns 200. If the health check fails, stop and keep the old version.&lt;/p&gt;

&lt;p&gt;A fast rollback is the other half of a zero-downtime deploy. With rolling, keeping the old code and reloading back is enough; with blue-green, just switch the Nginx upstream back to the old version and reload Nginx. Note: if a migration already dropped a column the old code still uses, the rollback will fail — so always drop columns in a separate deploy, after the new code has been running fine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It to Your Project
&lt;/h2&gt;

&lt;p&gt;For the expense tracker, start with a rolling deploy: add ExecReload to the Gunicorn service, then change the episode 17 deploy script from restart to reload — one line of change and the downtime is gone. As users grow, move to blue-green with ports 8001 and 8002, and write the process into a runbook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Posts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/13/django-in-production-17-ci-cd-with-github-actions-automated-tests-then-deploy/" rel="noopener noreferrer"&gt;Django in Production #17: CI/CD with GitHub Actions — Automated Tests, Then Deploy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/02/django-production-operations-6-safe-migrations-in-production-backward-compatible-patterns-zero-downtime-deploys/" rel="noopener noreferrer"&gt;Django Production Operations #6: Safe Migrations in Production — Backward-Compatible Patterns, Zero-Downtime Deploys&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linhfish.dev/en/2026/11/12/django-in-production-16-docker-for-production-multi-stage-builds-non-root-runtime-healthchecks/" rel="noopener noreferrer"&gt;Django in Production #16: Docker for Production — Multi-Stage Builds, Non-Root Runtime, Healthchecks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How is a rolling deploy different from blue-green?
&lt;/h3&gt;

&lt;p&gt;Rolling replaces workers one at a time on the same app — simple and enough for one server. Blue-green runs two complete versions side by side and switches traffic with Nginx — instant rollback, but it costs double the resources during the switch.&lt;/p&gt;

&lt;h3&gt;
  
  
  When should migrations run for zero downtime?
&lt;/h3&gt;

&lt;p&gt;Before the restart or reload, and migrations must be backward compatible: old code still runs on the new schema. Add a nullable column this time, clean up the old column next time.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if the health check fails?
&lt;/h3&gt;

&lt;p&gt;Stop the deploy immediately and keep the old version. With rolling, the old workers are still serving, so users are unaffected; with blue-green, just don’t switch the upstream. Then read the new version’s logs, fix the bug, and deploy again.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.gunicorn.org" rel="noopener noreferrer"&gt;Gunicorn — docs.gunicorn.org&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.djangoproject.com" rel="noopener noreferrer"&gt;Django deployment checklist — docs.djangoproject.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nginx.org" rel="noopener noreferrer"&gt;Nginx ngx_http_upstream_module — nginx.org&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://linhfish.dev/en/2026/11/14/django-in-production-18-zero-downtime-deploys-blue-green-rolling-migration-order-and-restarts/" rel="noopener noreferrer"&gt;linhfish.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>devops</category>
      <category>deployment</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
