django change default runserver port

265,300

Solution 1

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

Solution 2

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/

Solution 3

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)

Solution 4

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

Solution 5

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.

Share:
265,300

Related videos on Youtube

jonny
Author by

jonny

Updated on July 15, 2022

Comments

  • jonny
    jonny almost 2 years

    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.

    • Jonathan Hartley
      Jonathan Hartley almost 9 years
      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.
  • jonny
    jonny almost 10 years
    Either that, or I am thinking about adding a custom management command.
  • fixmycode
    fixmycode almost 10 years
    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.
  • jonny
    jonny almost 10 years
    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.
  • fixmycode
    fixmycode almost 10 years
    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...
  • PurpleDiane
    PurpleDiane almost 8 years
    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.
  • Flimm
    Flimm over 7 years
    @PurpleDiane The indentation is fixed.
  • arogachev
    arogachev over 7 years
    Modifying the sources of dependent packages is considered bad practice. Changes can be lost on the update / reinstalling.
  • Feuermurmel
    Feuermurmel about 7 years
    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.
  • Amrendra
    Amrendra about 7 years
    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.
  • Admin
    Admin almost 7 years
    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.
  • NaturalBornCamper
    NaturalBornCamper about 6 years
    My manage.py doesn't really look like that, I guess Django changed since
  • Vargas
    Vargas about 6 years
    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_addr
  • physicalattraction
    physicalattraction almost 6 years
    This answer is about changing the port, not changing the default port.
  • physicalattraction
    physicalattraction almost 6 years
    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?
  • Feuermurmel
    Feuermurmel almost 6 years
    @physicalattraction Your file is probably not in the right location. docs.djangoproject.com/en/2.0/howto/custom-management-comman‌​ds 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.
  • physicalattraction
    physicalattraction almost 6 years
    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.
  • x-yuri
    x-yuri over 5 years
    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.
  • Aderemi Dayo
    Aderemi Dayo about 5 years
    The 0.0.0.0 is important especially if you are running python inside docker container
  • JuSTMOnIcAjUSTmONiCAJusTMoNICa
    JuSTMOnIcAjUSTmONiCAJusTMoNICa almost 5 years
    "The goal is to run ./manage.py runserver without having to specify address and port every time"
  • Sathindu
    Sathindu about 4 years
    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.
  • E.Serra
    E.Serra about 3 years
    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 python
  • basquiatraphaeu
    basquiatraphaeu almost 3 years
    Also, make sure to add your port to the ALLOWED_HOSTS variable.