django change default runserver port
Asked Answered
E

19

232

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

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

Eirene answered 13/5, 2014 at 18:39 Comment(1)
If you run the command from your command line history, then it's even fewer keystrokes. E.g. for me it's "<ctrl-r>runs<enter>", and it automatically runs the same command as last time, so the port etc is included.Endoblast
M
273

create a bash script with the following:

#!/bin/bash
exec ./manage.py runserver 0.0.0.0:<your_port>

save it as runserver in the same dir as manage.py

chmod +x runserver

and run it as

./runserver
Migrant answered 13/5, 2014 at 19:14 Comment(7)
Either that, or I am thinking about adding a custom management command.Eirene
You can't run the development server programmatically, so a custom command won't work unless it calls the shell using something like call. If this solution works for you please make sure to mark it as solved.Migrant
I actually started using supervisor for this now, that makes it even easier to manage. :-) But yours is probably the cleanest solution other than writing a dedicated runserver script.Eirene
Supervisor is an good solution for I wouldn't recommend it to run the development environment. You lose the advantage of having the server output on the terminal, among other things. If you really want to use supervisor my advice would be to use it with a fully featured WSGI server like Gunicorn. Please don't run the development server as your production server...Migrant
This is useful but not greate when juggling multiple projects at a time - I would have accepted the answer below which specifies the port to be used for each distinct project. Just my opinion tho.Chloris
The 0.0.0.0 is important especially if you are running python inside docker containerHabakkuk
Also, make sure to add your port to the ALLOWED_HOSTS variable.Unequal
B
219

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

python manage.py runserver 7000

that should run development server on http://127.0.0.1:7000/

Boccherini answered 24/4, 2017 at 23:23 Comment(2)
This answer is about changing the port, not changing the default port.Blinker
This should be the correct answer, accepted one also implies the IP Address, and executing manage.py for some reason, instead of calling it via pythonFranciskus
S
69

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 manage.py which dynamically modify the default port number before invoking the runserver command:

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

    import django
    django.setup()

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

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)
Shannanshannen answered 12/7, 2016 at 3:13 Comment(4)
Although it doesn't answer the original question exactly, and the indenting is messed up (code should be indented from "import django" on down), I prefer this answer because it is entirely self-contained and does not require changing what is entered on the command line.Alina
@Alina The indentation is fixed.Ephesus
My manage.py doesn't really look like that, I guess Django changed sinceMormon
Currently (as of 2.0.3) you can just add: from django.core.management.commands.runserver import Command as runserver; runserver.default_port = "8080 to your manage.py. You can also change the listening address with: runserver.default_addrGovernance
A
46

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

python manage.py runserver 127.0.0.1:7000

python manage.py runserver 7000

python manage.py runserver 0:7000
Ascarid answered 19/3, 2018 at 10:37 Comment(1)
"The goal is to run ./manage.py runserver without having to specify address and port every time"Jalapa
S
14

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

from django.conf import settings
from django.core.management.commands 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.

Singe answered 27/4, 2017 at 10:22 Comment(4)
This seems like the best solution, however, my Django 1.8.14 doesn't recognize my self-created file runserver.py. Should I register it somewhere?Blinker
@Blinker Your file is probably not in the right location. docs.djangoproject.com/en/2.0/howto/custom-management-commands starts with a description of where to put the python module for the runserver command. Also maybe you have another app also registering a command named runserver. Try renaming your command and see whether it is recognized.Singe
I put it in <app_name>/management/commands/runserver.py, but then Django's original runserver is used. When I rename it to run_server.py, it is recognized. I don't see anything special about runserver on the page you link to.Blinker
The thing is staticfiles does exactly what you suggest. So following your instructions breaks serving static files in development. It's best to import from django.contrib.staticfiles.management.commands.Lili
S
12

you can try to add an argument in manage.py like this

python manage.py runserver 0.0.0.0:5000 

python manage.py runserver <your IP>:<port>

or you pass the port like this

python manage.py runserver 5000

python manage.py runserver <your port>

Stomato answered 7/5, 2021 at 2:9 Comment(0)
F
12

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


from django.core.management.commands.runserver import Command as runserver
runserver.default_port = "8000"
Fridafriday answered 6/6, 2022 at 12:53 Comment(1)
Yup working in 4.2.2Fictionist
C
10

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/runserver.py and put in something like this:

# Override the value of the constant coded into django...
import django.core.management.commands.runserver as runserver
runserver.DEFAULT_PORT="8001"

# ...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 manage.py runserver behaves normally in all other regards.
from django.core.management.commands.runserver import Command
Cameroun answered 6/7, 2015 at 11:40 Comment(0)
I
9

In Pycharm you can simply add the port to the parameters

enter image description here

Indorse answered 8/8, 2018 at 15:40 Comment(0)
I
8
python manage.py runserver <your port>

after in browser run 127.0.0.1:(your port)

Irrelevant answered 25/8, 2021 at 7:45 Comment(0)
V
6

in your project manage.py file add

from django.core.management.commands.runserver import Command as runserver

then in def main():

runserver.default_port = "8001" 
Virile answered 27/6, 2022 at 17:16 Comment(0)
A
3

Use django-extensions (with Werkzeug) and then simply put into your settings

RUNSERVERPLUS_SERVER_ADDRESS_PORT = '0.0.0.0:6000'

Example output

> python manage.py runserver_plus
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:6000
 * Running on http://192.168.2.8:6000

You can do it also with vanilla Django with some suggestions from others. But I've found using django-extensions is a must to be productive anyway.

Aflcio answered 17/5, 2023 at 8:40 Comment(1)
This works. I recommend not running on all addresses though: RUNSERVERPLUS_SERVER_ADDRESS_PORT = '127.0.0.1:6000'Lure
C
2

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.

Cashman answered 14/4, 2016 at 1:2 Comment(0)
L
2
  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
runserver
Lida answered 16/8, 2016 at 2:9 Comment(0)
F
1

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 runserver.py file in nano editor as superuser

     $ sudo nano runserver.py
    
  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

Frontlet answered 18/6, 2018 at 16:19 Comment(0)
T
1

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

from decouple import config
import django.core.management.commands.runserver 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

[settings]
WebServer_Port=8091

If not, delete this parameter.

Tradition answered 10/1, 2022 at 7:48 Comment(0)
B
-1

run this command

python .\manage.py runserver 8080

8080 is your port you can change it :)

Bedpan answered 10/9, 2023 at 17:31 Comment(0)
P
-2

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 runserver.py 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 manage.py 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

Palpate answered 11/10, 2014 at 16:54 Comment(2)
This is the worst suggestion of all here. IMHO, editing a file from a distribution is never a good idea and leads to confusion because the change is not tracked by a VCS and gets easily overwritten.Singe
It's not recommended to edit the Django module at the site packages. It can be updated by new version. Also it affect to all Django apps.Limpkin
L
-2

I was struggling with the same problem and found one solution. I guess it can help you. when you run python manage.py runserver, it will take 127.0.0.1 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>\Lib\site-packages\django\core\management\commands\runserver.py and set 1. default_port = '<your_port>'
2. find this under def handle and set
if not options.get('addrport'): self.addr = '0.0.0.0' self.port = self.default_port

Now if you run "python manage.py runserver" it will run by default on "0.0.0.0:

Enjoy coding .....

Litta answered 29/6, 2016 at 8:28 Comment(2)
Modifying the sources of dependent packages is considered bad practice. Changes can be lost on the update / reinstalling.Schellens
That's right @arogachev, It is just an option by which you can make default port and host. But it's really not a good practice to modify dependent packages. you can set the IP and port when you run your server in command prompt as well.Litta

© 2022 - 2024 — McMap. All rights reserved.