Django is an incredibly powerful, high-level Python web framework that fosters rapid development and clean, pragmatic design. It is equipped with various tools and features right out of the box. However, in certain instances, you may want to adjust the settings and configurations of Django to better suit your needs, such as changing the default port it runs on.
By default, when you start a new Django project, it will be configured to run on port 8000. But what if that port is already in use or if you simply prefer to use a different port? Here are a few ways you can change the port Django runs on.
Change Port Using the runserver Command
Changing the port using the `runserver` command is the easiest way to do it. All you need to do is specify the port number when you run the `runserver` command.
Firstly, navigate to your project’s root directory (the folder containing the `manage.py` file). From there, you can run the following command to change the port:
1 | python manage.py runserver <your_desired_port> |
Replace <your_desired_port> with the port number you want Django to run on. For instance, if you want to change the port to 9000, the command will be:
python manage.py runserver 9000
You can also run Django on all interfaces to allow access on network.
python manage.py runserver 0.0.0.0:9000
The 0.0.0.0 is the IP address on which Django will be available. By specifying 0.0.0.0, you’re ensuring Django will be accessible from any IP address, not just localhost.
Change Port Permanently in Django Settings
You might find the need to change the port permanently instead of specifying it every time you run the server. In this case, you can modify the settings of Django to use a different default port.
The Django settings do not directly provide a configuration for the default server port. Therefore, to permanently change the default port, we will have to create a new command script in Django.
- First, navigate to your project’s root directory (the directory that contains `manage.py` file).
- Inside your application folder, create a new directory named `management`.
- Inside the management directory, create another directory named `commands`.
- Inside the commands directory, create a new Python file named `runserver.py`. Your directory structure should look like this:123456789101112131415161718myproject/├── manage.py├── myapp/│ ├── __init__.py│ ├── management/│ │ ├── __init__.py│ │ ├── commands/│ │ │ ├── __init__.py│ │ │ ├── runserver.py│ ├── views.py│ ├── models.py│ └── ...├── myproject/│ ├── __init__.py│ ├── settings.py│ ├── urls.py│ └── ...└── ...
- In the `runserver.py` file, you’re going to override the default `runserver` command with the following script:123from django.core.management.commands.runserver import Command as runserverclass Command(runserver):default_port = "<your_desired_port>"
Replace <your_desired_port> with the port number you want Django to run on. For instance, if you want to change the port to 9000, the script will be:
123from django.core.management.commands.runserver import Command as runserverclass Command(runserver):default_port = "9000" - Save your changes. Now every time you run `python manage.py runserver`, Django will default to using the port specified in the `runserver.py` script.
Please note that by using the second method, you will be changing the default settings of your Django application, which may have some implications down the line. Be sure to fully understand these changes before applying them.
As you can see, changing the port Django runs on is not a difficult task. It’s simply a matter of using the runserver command with a specific port or changing the runserver.py script in your Django application.