Django change port launches by default

I would like to make a default port that manage.py runserver listens for as defined in a manage.py runserver config.ini . Is there an easier fix than parsing sys.argv inside manage.py and inserting a configured port?

The goal is to run ./manage.py runserver without having to specify an address and port each time, but it takes arguments from config.ini .

+43
python django django-manage.py manage.py
May 13 '14 at 18:39
source share
9 answers

create a bash script with the following:

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

save it as runerver in the same directory as manage.py

 chmod +x runserver 

and run it like

 ./runserver 
+77
May 13 '14 at 19:14
source share

As in Django 1.9, the simplest solution I found (based on the Quentin Stafford-Fraser solution) is to add a few lines to manage.py that dynamically change the default port number before calling the runserver :

 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) 
+14
Jul 12 '16 at 3:13
source share

Actually the easiest way to change (only) the port in developing a Django server is:

 python manage.py runserver 7000 

which should start the development server at http://127.0.0.1:7000/

+13
Apr 24 '17 at 23:23
source share

We have created a new runerver management command, which is a thin shell around the standard one, but it changes the default port. Roughly, you create management/commands/runserver.py and add 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 
+6
Jul 06 '15 at 11:40
source share

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 "runningerver" command, you start from the preferred port:

  • Find your python installation. (you may have several pythons installed, and you may also have a version of your virtual environment, so make sure you find the right one)
  • Inside the python folder, find the site-packages folder. Inside you will find your django installation.
  • Open the django-> core → management → commands folder
  • Inside the command folder, open the runningerver.py script file with a text editor
  • Find the DEFAULT_PORT field. it defaults to 8000. Change it to whatever you like DEFAULT_PORT = "8080"
  • Reboot the server: python manage.py runningerver and make sure it uses your installed port number.

It works with python 2.7, but it should also work with newer versions of python. Good luck.

+4
Oct 11 '14 at 16:54
source share

I'm very late to the party here, but if you are using an IDE like PyCharm, there is the option "Edit configurations" in the "Run" menu ("Run"> "Edit configurations"), where you can specify the default port. This, of course, is relevant only if you are debugging / testing through PyCharm.

+1
Apr 14 '16 at 1:02
source share

Subclass django.core.management.commands.runserver.Command and overwrite the default_port element. Save the file as your own management command, for example. 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 load the default port form settings (which in turn read other configuration files), but you can just read it from another file directly.

+1
Apr 27 '17 at 10:22
source share
  • Create an environment variable in your .bashrc

    export RUNSERVER_PORT = 8010

  • Create an alias

    alias runningerver = 'django-admin runningerver $ RUNSERVER_PORT'

Im using zsh and virtualenvs wrapper. I am adding export to projects postactivate script and asign port for each project.

 workon someproject runserver 
0
Aug 16 '16 at 2:09 on
source share

I struggled with the same problem and found one solution. I think this can help you. when you run python manage.py runserver as the default IP address will be 127.0.0.1 and 8000 as the default port number that can be configured in your python environment. In python setup go to <your python env>\Lib\site-packages\django\core\management\commands\runserver.py and set 1. default_port = '<your_port>'
2. Locate this under the def descriptor 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 runningerver", it will run by default at "0.0.0.0:

Enjoy the coding .....

-3
Jun 29 '16 at 8:28
source share



All Articles