django更改默认的runserver端口

我想使manage.py runserver在一个无关的config.ini监听可指定的默认端口。 有没有比parsingsys.argvmanage.py和插入configuration的端口更容易的修复?

我们的目标是运行./manage.py runserver而不必每次都指定地址和端口,而是让它从config.ini获取参数。

创build一个bash脚本,如下所示:

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

将其作为runserver保存在与manage.py相同的目录中

 chmod +x runserver 

并运行它

 ./runserver 

从Django 1.9开始,我find的最简单的解决scheme(基于Quentin Stafford-Fraser的解决scheme)是在manage.py添加几行,在调用runserver命令之前dynamic修改默认端口号:

 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) 

实际上,在开发Django服务器中更改(仅)端口的最简单的方法就是:

 python manage.py runserver 7000 

应该在http://127.0.0.1:7000/上运行开发服务器;

我们创build了一个新的“runserver”pipe理命令,这个命令是一个简单的包装器,但是改变了默认的端口。 粗略地说,你创buildmanagement/commands/runserver.py并把这样的东西:

 # 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 

这是一个旧post,但对于那些有兴趣的人:

如果您想更改默认的端口号,那么当您运行“runserver”命令时,首先使用您的首选端口来执行此操作:

  1. find你的python安装。 (你可以安装多个python,你也可以有你的虚拟环境版本,所以确保你find了正确的)
  2. 在python文件夹里findsite-packages文件夹。 里面,你会发现你的Django的安装
  3. 打开django文件夹 – >核心 – >pipe理 – >命令
  4. 在命令文件夹内用文本编辑器打开runserver.py脚本
  5. findDEFAULT_PORT字段。 它默认等于8000。 将其改为任何你喜欢的DEFAULT_PORT = "8080"
  6. 重新启动你的服务器:python manage.py runserver,看它是否使用你设置的端口号

它适用于Python 2.7,但它也应该与更新版本的Python。 祝你好运

我在这里参加聚会的时间已经很晚了,但是如果你使用PyCharm这样的IDE,在“运行”菜单(运行>编辑configuration)下的“编辑configuration”中有一个选项,你可以指定一个默认的端口。 这当然只有在通过PyCharm进行debugging/testing时才有意义。

创build一个django.core.management.commands.runserver.Command的子类并覆盖default_port成员。 将文件另存为您自己的pipe理命令,例如,在<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 

我在这里加载了默认的端口表单设置(它依次读取其他configuration文件),但是您也可以直接从其他文件中读取它。

  1. 在.bashrc中创build环境variables

    导出RUNSERVER_PORT = 8010

  2. 创build别名

    别名runserver ='django-admin runserver $ RUNSERVER_PORT'

我使用zsh和virtualenvs包装。 我把项目导出到每个项目的postactivate脚本和asign port。

 workon someproject runserver 

我正在同样的问题挣扎,发现一个解决scheme。 我想这可以帮助你。 当你运行python manage.py runserver ,默认的ip地址是127.0.0.1,默认的端口号是8000,可以在你的python环境下configuration。 在你的Python设置中,进入<your python env>\Lib\site-packages\django\core\management\commands\runserver.py并设置1. default_port = '<your_port>'
2.在def手柄下find并设置
if not options.get('addrport'): self.addr = '0.0.0.0' self.port = self.default_port

现在如果你运行“python manage.py runserver”,默认运行“0.0.0.0:

享受编码…..