django change default runserver port
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.
Related videos on Youtube
jonny
Updated on July 15, 2022Comments
-
jonny almost 2 years
I would like to make the default port that
manage.py runserver
listens on specifiable in an extraneousconfig.ini
. Is there an easier fix than parsingsys.argv
insidemanage.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 theconfig.ini
.-
Jonathan Hartley almost 9 yearsIf 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 almost 10 yearsEither that, or I am thinking about adding a custom management command.
-
fixmycode almost 10 yearsYou 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 almost 10 yearsI 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 dedicatedrunserver
script. -
fixmycode almost 10 yearsSupervisor 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 almost 8 yearsAlthough 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 over 7 years@PurpleDiane The indentation is fixed.
-
arogachev over 7 yearsModifying the sources of dependent packages is considered bad practice. Changes can be lost on the update / reinstalling.
-
Feuermurmel about 7 yearsThis 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 about 7 yearsThat'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 almost 7 yearsThis 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 about 6 yearsMy manage.py doesn't really look like that, I guess Django changed since
-
Vargas about 6 yearsCurrently (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 almost 6 yearsThis answer is about changing the port, not changing the default port.
-
physicalattraction almost 6 yearsThis 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 almost 6 years@physicalattraction 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 namedrunserver
. Try renaming your command and see whether it is recognized. -
physicalattraction almost 6 yearsI put it in
<app_name>/management/commands/runserver.py
, but then Django's originalrunserver
is used. When I rename it torun_server.py
, it is recognized. I don't see anything special aboutrunserver
on the page you link to. -
x-yuri over 5 yearsThe thing is
staticfiles
does exactly what you suggest. So following your instructions breaks serving static files in development. It's best to import fromdjango.contrib.staticfiles.management.commands
. -
Aderemi Dayo about 5 yearsThe
0.0.0.0
is important especially if you are running python inside docker container -
JuSTMOnIcAjUSTmONiCAJusTMoNICa almost 5 years"The goal is to run ./manage.py runserver without having to specify address and port every time"
-
Sathindu about 4 yearsIt'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 about 3 yearsThis 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 almost 3 yearsAlso, make sure to add your port to the ALLOWED_HOSTS variable.