django change default runserver port


I would like to make the default port that runserver listens on specifiable in an extraneous config.ini. Is there an easier fix than parsing sys.argv inside and inserting the configured port?

The goal is to run ./ runserver without having to specify address and port every time but having it take the arguments from the config.ini.

Asked By: jonny



create a bash script with the following:

exec ./ runserver<your_port>

save it as runserver in the same dir as

chmod +x runserver

and run it as

Answered By: fixmycode

This is an old post but for those who are interested:

If you want to change the default port number so when you run the “runserver” command you start with your preferred port do this:

  1. Find your python installation. (you can have multiple pythons installed and you can have your virtual environment version as well so make sure you find the right one)
  2. Inside the python folder locate the site-packages folder. Inside that you will find your django installation
  3. Open the django folder-> core -> management -> commands
  4. Inside the commands folder open up the script with a text editor
  5. Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like
    DEFAULT_PORT = "8080"
  6. Restart your server: python runserver and see that it uses your set port number

It works with python 2.7 but it should work with newer versions of python as well. Good luck

Answered By: Daniel Kiss

We created a new ‘runserver’ management command which is a thin wrapper around the standard one but changes the default port. Roughly, you create management/commands/ and put in something like this:

# Override the value of the constant coded into django...
import as runserver

# ...print out a warning...
# (This gets output twice because runserver fires up two threads (one for autoreload).
#  We're living with it for now :-)
import os
dir_path = os.path.splitext(os.path.relpath(__file__))[0]
python_path = dir_path.replace(os.sep, ".")
print "Using %s with default port %s" % (python_path, runserver.DEFAULT_PORT)

# ...and then just import its standard Command class.
# Then runserver behaves normally in all other regards.
from import Command

I’m very late to the party here, but if you use an IDE like PyCharm, there’s an option in ‘Edit Configurations’ under the ‘Run’ menu (Run > Edit Configurations) where you can specify a default port. This of course is relevant only if you are debugging/testing through PyCharm.

Answered By: Smitty

I was struggling with the same problem and found one solution. I guess it can help you.
when you run python runserver, it will take as default ip address and 8000 as default port number which can be configured in your python environment.
In your python setting, go to <your python env> and set
1. default_port = '<your_port>'
2. find this under def handle and set
if not options.get('addrport'):
self.addr = ''
self.port = self.default_port

Now if you run “python runserver” it will run by default on “

Enjoy coding …..

Answered By: Amrendra

As of Django 1.9, the simplest solution I have found (based on Quentin Stafford-Fraser’s solution) is to add a few lines to which dynamically modify the default port number before invoking the runserver command:

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

    import django

    # Override default port for `runserver` command
    from import Command as runserver
    runserver.default_port = "8080"

    from import execute_from_command_line

Answered By: BillyBBone
  1. Create enviroment variable in your .bashrc

    export RUNSERVER_PORT=8010

  2. Create alias

    alias runserver=’django-admin runserver $RUNSERVER_PORT’

Im using zsh and virtualenvs wrapper. I put export in projects postactivate script and asign port for every project.

workon someproject
Answered By: Pavel1114

Actually the easiest way to change (only) port in development Django server is just like:

python runserver 7000

that should run development server on

Answered By: Qback

Create a subclass of and overwrite the default_port member. Save the file as a management command of your own, e.g. under <app-name>/management/commands/

from django.conf import settings
from import runserver

class Command(runserver.Command):
    default_port = settings.RUNSERVER_PORT

I’m loading the default port form settings here (which in turn reads other configuration files), but you could just as well read it from some other file directly.

Answered By: Feuermurmel

All of the following commands are possible to change the port while running django:

python runserver

python runserver 7000

python runserver 0:7000

If you wish to change the default configurations then follow this steps:

  1. Open terminal type command

     $ /usr/local/lib/python<2/3>.x/dist-packages/django/core/management/commands
  2. Now open file in nano editor as superuser

     $ sudo nano
  3. find the ‘default_port’ variable then you will see the default port no is ‘8000’. Now you can change it to whatever you want.

  4. Now exit and save the file using “CTRL + X and Y to save the file”

Note: Replace <2/3>.x with your usable version of python

Answered By: rhoitjadhav

In Pycharm you can simply add the port to the parameters

enter image description here

Answered By: chaggy

you can try to add an argument in like this

python runserver 

python runserver <your IP>:<port>

or you pass the port like this

python runserver 5000

python runserver <your port>

Answered By: perymerdeka

first you apply the migrations for app

python migrate


python runserver <your port>

after in browser run port)

Answered By: thHENRY

For Django 3.x, just change default_port in Like this:

from decouple import config
import as runserver

runserver.Command.default_port = config('WebServer_Port', default = "8088")

Then, if you want to specify the port, just add a new line in your setting.ini


If not, delete this parameter.

Answered By: beibeitu

in the last version of Django(Right now: 4.0.3), you can add these lines to your file

from import Command as runserver
runserver.default_port = "8000"
Answered By: BGOPC

in your project file add

from import Command as runserver

then in def main():

runserver.default_port = "8001" 
Answered By: Ehsäɳ Khʌɳ