我想在一个多余的config.ini中指定manage.py runserver侦听的默认端口。有没有比解析sys. exe更简单的解决方案?在manage.py和插入配置端口Argv ?
目标是运行./manage.py runserver,而不必每次都指定地址和端口,而是让它从config.ini中获取参数。
我想在一个多余的config.ini中指定manage.py runserver侦听的默认端口。有没有比解析sys. exe更简单的解决方案?在manage.py和插入配置端口Argv ?
目标是运行./manage.py runserver,而不必每次都指定地址和端口,而是让它从config.ini中获取参数。
当前回答
您可以尝试像这样在manage.py中添加一个参数
python manage.py runserver 0.0.0.0:5000
python manage.py runserver <您的IP>:<端口>
或者像这样通过港口
python manage.py runserver 5000
Python manage.py runserver <您的端口>
其他回答
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 "上:
享受编码.....
用下面的代码创建一个bash脚本:
#!/bin/bash
exec ./manage.py runserver 0.0.0.0:<your_port>
将其保存为与manage.py相同目录下的runserver
chmod +x runserver
然后运行它
./runserver
我在这里很晚了,但如果你使用像PyCharm这样的IDE,在“运行”菜单下的“编辑配置”中有一个选项(运行>编辑配置),在那里你可以指定一个默认端口。当然,这只适用于通过PyCharm进行调试/测试。
在Pycharm中,可以简单地将端口添加到参数中
下面所有的命令都可以在运行django时改变端口:
python manage.py runserver 127.0.0.1:7000
python manage.py runserver 7000
python manage.py runserver 0:7000