Open In App

Change the Django Default Runserver Port

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Changing the default run server port in Django is a simple yet useful adjustment for various development scenarios. By specifying a different port, you can avoid conflicts, manage multiple projects, and test your application in different environments. Remember to adjust your development and testing practices as needed to accommodate the new port settings.

Changing the Default Port in Django

To change the default port of the Django development server, you need to provide a different port number when running the runserver command. Here’s how you can do it:

Basic Command Structure

The general syntax for running the Django development server on a different port is:

python manage.py runserver <port_number>

Example: Changing the Port to 8080

To run the server on port 8080 instead of the default 8000, use the following command:

python manage.py runserver 8080

When you run this command, Django will start the development server on http://127.0.0.1:8080/.

Specifying a Host and Port

You can also specify both the host and the port if needed. For example, to run the server on port 8080 and make it accessible from any IP address (useful for testing on a local network), use:

python manage.py runserver 0.0.0.0:8080

In this case, Django will listen on all available network interfaces at port 8080, making it accessible from other devices on your local network.

Checking the Server Status

After starting the server with a new port, you should see an output indicating that the server is running on the specified port. For example:

Django version 4.2, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.

Alternative Method: Using Environment Variables

By using environment variables, you can set the port number dynamically without hardcoding it into your runserver command. This method is beneficial for setting up development or staging environments where the port may need to be adjusted frequently.

1. Set Environment Variable

To change the port using an environment variable, you can define a variable that specifies the desired port number. For instance, you can set an environment variable DJANGO_PORT to the port number you want.

On Linux/macOS, you can set this environment variable in your shell:

export DJANGO_PORT=8080

On Windows Command Prompt, you can use:

set DJANGO_PORT=8080

On Windows PowerShell, you can use:

$env:DJANGO_PORT = "8080"

2. Modify manage.py or wsgi.py

To make Django use this environment variable, you'll need to modify the manage.py file (or wsgi.py if you're deploying). Open manage.py and make the following changes:

Replace:

from django.core.management import execute_from_command_line

if __name__ == "__main__":
execute_from_command_line(sys.argv)

With:

import os
import sys
from django.core.management import execute_from_command_line

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.dev")

# Check if DJANGO_PORT is set and append it only for runserver
if len(sys.argv) > 1 and sys.argv[1] == "runserver":
port = os.getenv('DJANGO_PORT', '8000')
sys.argv.append(f"0.0.0.0:{port}")

execute_from_command_line(sys.argv)

This modification allows manage.py to:

  • Read the port from the DJANGO_PORT environment variable.
  • Preserves the ability to run other Django commands.
  • Dynamically appends the port only when running runserver.
  • Uses os.getenv('DJANGO_PORT', '8000') to allow environment-based configuration.

3. Run the Server

With the environment variable set and the manage.py file modified, you can now run the Django development server as usual:

python manage.py runserver

Django will use the port specified in the DJANGO_PORT environment variable. If the variable is not set, it will default to port 8000.


Article Tags :
Practice Tags :

Similar Reads