Changing the Default runserver Port in Django

When you start up the development server in Django using the python manage.py runserver command, it will by default bind to port 8000 on the local machine. However, sometimes you may want to change this default port for various reasons. Luckily, Django makes it easy to customize the port used by the runserver command.

Why Change the DefaultPort?

There are a few common reasons you may want to use a differentPort than 8000 for your Django project in development:

Avoiding Port Conflicts

For example, if you already have another application or service running onPort 8000 on your local system, it will conflict with Django’s built-in development server. Therefore, you need to configure Django to start on a different free port instead.

Matching Production Environment

Or, if your Django app in production runs on a differenPort, like port 80, you may want your local development server to match that for consistency while testing and debugging code changes.

Running Multiple Django Sites

Additionally, when working on multiple Django projects on the same system, you need separate ports for each to avoid interfering with each other.

So in all these cases, knowing how to change the defaultPort can help resolve these kinds of issues when working on Django projects.

How to Specify a Custom Port

Django makes overriding the defaulPort easy through a command line option when starting the server. There are a couple ways to specify thePort:

1. Using –port

The easiest approach is to pass the --port or -p option to runserver, followed by the desired port number:

python manage.py runserver --port=8001

This will start the development server onPort 8001 instead of 8000.

2. Define in Settings Module

You can also add aPORT variable in the Django settings module, typically settings.py:

PORT = 8001

Then when you start the server normally with python manage.py runserver, it will automatically bind to whatever port is defined inPORT.

Both these methods work to change the default port from 8000 to a custom port of your choice.

Other Runserver Customizations

In addition toPort changes, you can customize some other common aspects of Django’s development web server:

Binding to a Different IP

By default, runserver listens only on the local loopback interface at 127.0.0.1. To make your development server accessible from other machines, you can pass the 0.0.0.0 IP instead:

python manage.py runserver 0.0.0.0:8001

Launching from an External Script

You can also launch the dev server directly from a Python script, giving more control over initialization and configuration:

import os
from django.core.management import execute_from_command_line

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") 

execute_from_command_line(["manage.py", "runserver", "8002"])

This starts it onPort 8002 from an external script.

Conclusion

Being able to change the default Django runserverPort is useful for avoiding conflicts, matching production, or running multiple local sites. Both the --port option andPORT setting give control over the port binding. Additionally, other customizations like binding IP and external launching provide flexibility when running the development server. So explore all these options to configure your dev environment according to your particular needs.