我想在一个多余的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中获取参数。
当前回答
在上一个版本的Django(目前:4.0.3)中,你可以在settings.py文件中添加这些行
from django.core.management.commands.runserver import Command as runserver
runserver.default_port = "8000"
其他回答
在你的项目manage.py文件中添加
from django.core.management.commands.runserver import Command as runserver
然后在def main()中:
runserver.default_port = "8001"
我在这里很晚了,但如果你使用像PyCharm这样的IDE,在“运行”菜单下的“编辑配置”中有一个选项(运行>编辑配置),在那里你可以指定一个默认端口。当然,这只适用于通过PyCharm进行调试/测试。
在.bashrc中创建环境变量 出口RUNSERVER_PORT = 8010 创建别名 alias runserver='django-admin runserver $RUNSERVER_PORT'
我使用zsh和virtualenvs包装。我把出口项目后激活脚本和分配端口为每个项目。
workon someproject
runserver
这是一个旧的帖子,但是对于那些感兴趣的人:
如果你想改变默认端口号,那么当你运行"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。祝你好运
创建django.core.management.commands.runserver.Command的子类,并覆盖default_port成员。将文件保存为您自己的管理命令,例如在<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
我在这里加载默认的端口表单设置(它反过来读取其他配置文件),但您也可以直接从其他文件读取它。