How to change the Django default runserver port?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Django’s development server listens on port 8000 by default, but you can change the port every time you run it by passing a host and port argument. There is no built-in project setting that permanently changes the runserver default for all invocations. If you want a stable custom port, the usual solution is a command alias, an environment-specific script, or a custom management command.
Change The Port On The Command Line
The direct way is:
That starts the development server on port 8080 instead of 8000.
If you also want to control the bind address, specify host and port together:
That is especially common when testing from another device on the local network or when running inside a container.
What The Default Actually Is
When you run:
Django behaves roughly like “bind to localhost on port 8000.” That is a convenience default for development only. It is not a production server configuration.
So if the real question is “how do I make the server use a different port this one time,” the answer is the command-line argument. If the real question is “how do I permanently redefine the default,” then you need to add your own wrapper around the command.
A Simple Shell Alias Or Script
Many teams solve this with a shell alias or a small script rather than changing Django internals.
Or a script file:
This keeps the behavior explicit and avoids surprising other developers who expect Django’s normal defaults.
Using A Custom Management Command
If you really want project-level custom behavior, you can create your own management command that wraps runserver.
Then you could run:
That is usually better than trying to patch Django’s built-in runserver behavior globally.
Development Versus Production
It is important not to over-interpret the runserver port question. runserver is a development convenience server. In production, you normally use something like Gunicorn, uWSGI, Daphne, or another ASGI or WSGI deployment stack behind a reverse proxy.
So changing the Django dev-server port is a local workflow choice, not a production architecture decision.
When Binding To 0.0.0.0 Matters
If the server is inside Docker or you want other machines on your LAN to reach it, binding only to 127.0.0.1 is not enough.
This tells Django to listen on all interfaces. Combined with container port publishing or local firewall rules, that makes the dev server reachable externally.
A Good Rule Of Thumb
Use these patterns:
- one-off local port change:
python manage.py runserver 8080, - external access or Docker:
python manage.py runserver 0.0.0.0:8080, - repeatable team workflow: alias, script, or custom management command.
That covers most real-world development needs cleanly.
Common Pitfalls
- Looking for a Django setting in
settings.pythat permanently changesrunserver’s default port. - Forgetting that
runserver 8080changes only the port, not the host binding. - Using
127.0.0.1when the server needs to be reachable from outside the local machine. - Treating
runserveras a production deployment server. - Hiding team-specific port behavior in non-obvious local shell setup without documenting it.
Summary
- Change the Django dev-server port by passing it to
runserver, for examplepython manage.py runserver 8080. - Use
0.0.0.0:8080when external access or containers are involved. - Django does not provide a normal built-in setting that permanently changes the
runserverdefault port. - Use an alias, script, or custom management command if you want a repeatable project-specific port.
- Remember that
runserveris for development, not production.
Related reading
- How to change the figure size of a seaborn axes or figure level plot
- How to change the font size on a matplotlib plot
- How to change the font size on a matplotlib plot
- How to change the name of a Django app?
- How to change the order of DataFrame columns?
- How to change the order of DataFrame columns?
- How to check a string for specific characters?
- How to check Django version
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.