我想在一个多余的config.ini中指定manage.py runserver侦听的默认端口。有没有比解析sys. exe更简单的解决方案?在manage.py和插入配置端口Argv ?

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


当前回答

在Pycharm中,可以简单地将端口添加到参数中

其他回答

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

现在如果你运行"python manage.py runserver",它将默认运行在"0.0.0.0 "上:

享受编码.....

这是一个旧的帖子,但是对于那些感兴趣的人:

如果你想改变默认端口号,那么当你运行"runserver"命令时,你可以这样做:

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

它适用于python 2.7,但也适用于较新版本的python。祝你好运

在Pycharm中,可以简单地将端口添加到参数中

如果您希望更改默认配置,请执行以下步骤:

打开终端类型命令 美元/usr/local/lib/python < > 2/3。x / dist-packages / django /核心/管理/命令 现在以超级用户身份在nano编辑器中打开runserver.py文件 $ sudo nano runserver.py 找到'default_port'变量,然后你会看到默认端口是'8000'。现在你可以把它改成任何你想要的。 现在退出并保存文件使用“CTRL + X和Y来保存文件”

注意:替换<2/3>。X和你可用的python版本

首先你为app应用迁移

python manage.py migrate

然后:

python manage.py runserver <your port>

在浏览器运行127.0.0.1后:(你的端口)