Python Django 更改默认运行服务器端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23639085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:15:52  来源:igfitidea点击:

django change default runserver port

pythondjangodjango-manage.pymanage.py

提问by jonny

I would like to make the default port that manage.py runserverlistens on specifiable in an extraneous config.ini. Is there an easier fix than parsing sys.argvinside manage.pyand inserting the configured port?

我想让默认端口manage.py runserver在无关的config.ini. 有没有比sys.argv在内部解析manage.py并插入配置的端口更简单的解决方法?

The goal is to run ./manage.py runserverwithout having to specify address and port every time but having it take the arguments from the config.ini.

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

采纳答案by Pablo Albornoz

create a bash script with the following:

使用以下内容创建一个 bash 脚本:

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

save it as runserver in the same dir as manage.py

将其另存为 runserver 在与 manage.py 相同的目录中

chmod +x runserver

and run it as

并运行它

./runserver

回答by Daniel Kiss

This is an old post but for those who are interested:

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

If you want to change the default port number so when you run the "runserver" command you start with your preferred port do this:

如果要更改默认端口号,以便在运行“runserver”命令时从首选端口开始,请执行以下操作:

  1. 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)
  2. Inside the python folder locate the site-packages folder. Inside that you will find your django installation
  3. Open the django folder-> core -> management -> commands
  4. Inside the commands folder open up the runserver.py script with a text editor
  5. Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like DEFAULT_PORT = "8080"
  6. Restart your server: python manage.py runserver and see that it uses your set port number
  1. 找到你的python安装。(您可以安装多个python,也可以安装虚拟环境版本,因此请确保找到正确的版本)
  2. 在 python 文件夹中找到 site-packages 文件夹。在里面你会找到你的 django 安装
  3. 打开django文件夹->核心->管理->命令
  4. 在命令文件夹中,使用文本编辑器打开 runserver.py 脚本
  5. 找到 DEFAULT_PORT 字段。默认情况下等于 8000。将其更改为您喜欢的任何内容 DEFAULT_PORT = "8080"
  6. 重新启动您的服务器: python manage.py runserver 并查看它使用您设置的端口号

It works with python 2.7 but it should work with newer versions of python as well. Good luck

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

回答by Quentin Stafford-Fraser

We created a new 'runserver' management command which is a thin wrapper around the standard one but changes the default port. Roughly, you create management/commands/runserver.pyand put in something like this:

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

回答by Smitty

I'm very late to the party here, but if you use an IDE like PyCharm, there's an option in 'Edit Configurations' under the 'Run' menu (Run > Edit Configurations) where you can specify a default port. This of course is relevant only if you are debugging/testing through PyCharm.

我在这里参加聚会很晚,但是如果您使用像 PyCharm 这样的 IDE,则在“运行”菜单(运行 > 编辑配置)下的“编辑配置”中有一个选项,您可以在其中指定默认端口。这当然只有在您通过 PyCharm 进行调试/测试时才相关。

回答by Amrendra

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.pyand 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,默认IP地址为127.0.0.1,默认端口号为8000,可以在python环境中配置。在你的python设置中,转到<your python env>\Lib\site-packages\django\core\management\commands\runserver.py并设置1。2 default_port = '<your_port>'
.在def句柄下找到它并设置
if not options.get('addrport'): self.addr = '0.0.0.0' self.port = self.default_port

Now if you run "python manage.py runserver" it will run by default on "0.0.0.0:

现在,如果您运行“python manage.py runserver”,它将默认在“0.0.0.0”上运行:

Enjoy coding .....

享受编码......

回答by BillyBBone

As of Django 1.9, the simplest solution I have found (based on Quentin Stafford-Fraser's solution) is to add a few lines to manage.pywhich dynamically modify the default port number before invoking the runservercommand:

从 Django 1.9 开始,我发现的最简单的解决方案(基于 Quentin Stafford-Fraser 的解决方案)是manage.py在调用runserver命令之前添加几行动态修改默认端口号:

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.dev")

    import django
    django.setup()

    # Override default port for `runserver` command
    from django.core.management.commands.runserver import Command as runserver
    runserver.default_port = "8080"

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

回答by Pavel1114

  1. Create enviroment variable in your .bashrc

    export RUNSERVER_PORT=8010

  2. Create alias

    alias runserver='django-admin runserver $RUNSERVER_PORT'

  1. 在 .bashrc 中创建环境变量

    出口 RUNSERVER_PORT=8010

  2. 创建别名

    alias runserver='django-admin runserver $RUNSERVER_PORT'

Im using zsh and virtualenvs wrapper. I put export in projects postactivate script and asign port for every project.

我使用 zsh 和 virtualenvs 包装器。我将导出放在项目 postactivate 脚本中,并为每个项目指定端口。

workon someproject
runserver

回答by Qback

Actually the easiest way to change (only) port in development Django server is just like:

实际上,在开发 Django 服务器中更改(仅)端口的最简单方法就像:

python manage.py runserver 7000

that should run development server on http://127.0.0.1:7000/

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

回答by Feuermurmel

Create a subclass of django.core.management.commands.runserver.Commandand overwrite the default_portmember. Save the file as a management command of your own, e.g. under <app-name>/management/commands/runserver.py:

创建一个子类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

I'm loading the default port form settings here (which in turn reads other configuration files), but you could just as well read it from some other file directly.

我在这里加载默认的端口形式设置(它依次读取其他配置文件),但您也可以直接从其他文件中读取它。

回答by Krishnamoorthy Acharya

All of the following commands are possible to change the port while running django:

以下所有命令都可以在运行 django 时更改端口:

python manage.py runserver 127.0.0.1:7000

python manage.py runserver 7000

python manage.py runserver 0:7000