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

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


当前回答

我们创建了一个新的“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

其他回答

用下面的代码创建一个bash脚本:

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

将其保存为与manage.py相同目录下的runserver

chmod +x runserver

然后运行它

./runserver

首先你为app应用迁移

python manage.py migrate

然后:

python manage.py runserver <your port>

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

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

如果你想改变默认端口号,那么当你运行"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。祝你好运

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

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

workon someproject
runserver

我们创建了一个新的“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