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

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


当前回答

在.bashrc中创建环境变量 出口RUNSERVER_PORT = 8010 创建别名 alias runserver='django-admin runserver $RUNSERVER_PORT'

我使用zsh和virtualenvs包装。我把出口项目后激活脚本和分配端口为每个项目。

workon someproject
runserver

其他回答

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

我们创建了一个新的“runserver”管理命令,它是标准命令的精简包装,但改变了默认端口。粗略地说,你创建management/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

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

python manage.py runserver 7000

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

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

打开终端类型命令 美元/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版本

创建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

我在这里加载默认的端口表单设置(它反过来读取其他配置文件),但您也可以直接从其他文件读取它。