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.

Advertisement

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:

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.

  1. First, navigate to your project’s root directory (the directory that contains `manage.py` file).
  2. Inside your application folder, create a new directory named `management`.
  3. Inside the management directory, create another directory named `commands`.
  4. Inside the commands directory, create a new Python file named `runserver.py`. Your directory structure should look like this:

  5. In the `runserver.py` file, you’re going to override the default `runserver` command with the following script:

    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:

  6. 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.

Share.
Leave A Reply


Exit mobile version