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.
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)
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.
# 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 importCommand
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
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.
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:
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)
Inside the python folder locate the site-packages folder. Inside that you will find your django installation
Open the django folder-> core -> management -> commands
Inside the commands folder open up the runserver.py script with a text editor
Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like
DEFAULT_PORT = "8080"
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
回答 11
我在同一个问题上挣扎,找到了一个解决方案。我想它可以帮助您。运行时python manage.py runserver,它将在默认ip地址中使用127.0.0.1,在python环境中配置默认端口8000。在您的python设置中,转到<your python env>\Lib\site-packages\django\core\management\commands\runserver.py并设置1。2 default_port = '<your_port>'
.在def handle下找到并设置 if not options.get('addrport'):
self.addr = '0.0.0.0'
self.port = self.default_port
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: