Django: Remove the Admin from Production
The Reasoning First
Every Django project ships with /admin/ mounted and reachable. It is a login form for the most powerful interface on your site, and on a small site it is often the only place an attacker can attempt a password against a privileged account.
You cannot hide a URL effectively — it is in every tutorial, every django-admin startproject output, and any leaked urls.py. But you can make it not exist on the server at all, which removes the target rather than obscuring it.
This assumes the normal setup: you develop locally with DEBUG = True and deploy with DEBUG = False.

Step 1: A Setting That Follows DEBUG
In settings.py:
if DEBUG is False:
ADMIN_ENABLED = False
else:
ADMIN_ENABLED = True
Use is False, not == False. Django's DEBUG is a bool and the identity check is the clearer statement of intent.
Step 2: Conditional Mounting in urls.py
Remove path('admin/', admin.site.urls) from the top-level list and mount it conditionally instead:
from django.conf import settings
if settings.ADMIN_ENABLED:
urlpatterns += [
path('admin/', admin.site.urls),
]
Now a production request to /admin/ gets a 404, same as any other nonexistent path. There is no login form to attack, no password reset flow to abuse, and nothing in the response to confirm Django is running.
The django-cms Complication
If you use django-cms, its catch-all re_path(r'^', include('cms.urls')) swallows everything, so "just don't add it" does not work: the CMS will try to resolve /admin/ as a page and you get a confusing 404 from the CMS rather than from Django.
The fix is to build the whole urlpatterns per branch:
if not settings.ADMIN_ENABLED:
urlpatterns = [
path("", include("fleet.urls")),
path("", include("authentication.urls")),
re_path(r'^', include('cms.urls')),
]
if settings.ADMIN_ENABLED:
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("fleet.urls")),
path("", include("authentication.urls")),
re_path(r'^', include('cms.urls')),
]
Verbose, and duplication invites drift. If this grows, build a shared list and prepend the admin entry only when the flag is on. I have written it both ways; the explicit version is easier to read six months later, the shared list is easier to maintain.

What This Does and Does Not Buy You
It removes a login surface from the public internet, which removes password spraying, removes credential stuffing against admin accounts, and removes the class of bugs that only affect the admin interface.
It does not make your app secure on its own. The obvious problem: you now cannot log in to administer the site in production. Saying "we'll enable it temporarily when needed" means a redeploy every time you want to fix a typo.
The sane middle ground, in order of preference:
Keep the admin but require a second factor. django-otp with django-two-factor-auth puts a TOTP prompt in front of the login. This is the option I would pick for anything with real data.
Restrict by source address at the proxy. In nginx, deny /admin/ to everyone except your office or VPN range. The URL still exists, but not for the internet.
Move it to a non-obvious path with a random prefix. Weak, since the path leaks in any error trace, browser history or shared screen, but better than nothing and zero effort.
Disable it entirely in production — what this article does — and administer the site through management commands, a Django shell, or a separate internal tool.
Verify It Actually Went Away
Do not assume the conditional worked because the code reads correctly. Check production, from outside your network.
curl -s -o /dev/null -w "%{http_code}" https://your-site.example/admin/
Expect 404. A 302 to a login page means the admin is still mounted and something else is setting the flag — a stray ADMIN_ENABLED = True later in settings.py, or a local_settings.py imported after the block.
Then check that the rest of the site is unaffected:
curl -s -o /dev/null -w "%{http_code}" https://your-site.example/
A 500 here usually means urlpatterns was rebuilt incorrectly and something that used to be mounted is now missing. The django-cms branch in particular is easy to break by dropping an include.

A Note on the Attack Surface Argument
The reason this tip works is not that attackers cannot find your admin. It is that a 404 is a much better answer than a login form: there is nothing to enumerate, nothing to rate-limit, and nothing to patch in a hurry when a vulnerability lands in the admin views. Removing an interface from production is one of the few security changes with no ongoing maintenance cost.
Just do not confuse removing the front door with living in a safe house. Keep Django patched, keep SECRET_KEY out of source control, keep DEBUG false, and set SECURE_* settings properly. This tip is one layer, not the wall.
Don't trust the download button. Trust the hash.
If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.