问题:django更改默认运行服务器端口

我想在manage.py runserver侦听器中指定可侦听的默认端口config.ini。是否有比sys.argv在内部解析manage.py并插入配置的端口更容易的修复方法?

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

I would like to make the default port that manage.py runserver listens on specifiable in an extraneous config.ini. Is there an easier fix than parsing sys.argv inside manage.py and inserting the configured port?

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


回答 0

使用以下命令创建一个bash脚本:

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

将其保存为runserver并与manage.py放在同一目录中

chmod +x runserver

并运行为

./runserver

create a bash script with the following:

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

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

chmod +x runserver

and run it as

./runserver

回答 1

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

python manage.py runserver 7000

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

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

python manage.py runserver 7000

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


回答 2

从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)

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.py which dynamically modify the default port number before invoking the runserver command:

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)

回答 3

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

python manage.py runserver 127.0.0.1:7000

python manage.py runserver 7000

python manage.py runserver 0:7000

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

python manage.py runserver 127.0.0.1:7000

python manage.py runserver 7000

python manage.py runserver 0:7000

回答 4

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

我在此处加载默认端口表单设置(依次读取其他配置文件),但是您也可以直接从其他文件中读取它。

Create a subclass of django.core.management.commands.runserver.Command and overwrite the default_port member. Save the file as a management command of your own, e.g. under <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.


回答 5

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

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.py and put in something like this:

# 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

回答 6

在Pycharm中,您只需将端口添加到参数中

在此处输入图片说明

In Pycharm you can simply add the port to the parameters

enter image description here


回答 7

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

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.


回答 8

如果您想更改默认配置,请按照以下步骤操作:

  1. 打开终端类型命令

     $ /usr/local/lib/python<2/3>.x/dist-packages/django/core/management/commands
  2. 现在以超级用户身份在nano编辑器中打开runserver.py文件

     $ sudo nano runserver.py
  3. 找到“ default_port”变量,您将看到默认端口号为“ 8000”。现在,您可以将其更改为所需的任何内容。

  4. 现在退出并使用“ CTRL + X和Y保存文件”保存文件

注意:将<2/3> .x替换为可用的python版本

If you wish to change the default configurations then follow this steps:

  1. Open terminal type command

     $ /usr/local/lib/python<2/3>.x/dist-packages/django/core/management/commands
    
  2. Now open runserver.py file in nano editor as superuser

     $ sudo nano runserver.py
    
  3. find the ‘default_port’ variable then you will see the default port no is ‘8000’. Now you can change it to whatever you want.

  4. Now exit and save the file using “CTRL + X and Y to save the file”

Note: Replace <2/3>.x with your usable version of python


回答 9

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

    导出RUNSERVER_PORT = 8010

  2. 建立别名

    别名runserver =’django-admin runserver $ RUNSERVER_PORT’

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

workon someproject
runserver
  1. Create enviroment variable in your .bashrc

    export RUNSERVER_PORT=8010

  2. Create alias

    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.

workon someproject
runserver

回答 10

这是旧文章,但对那些感兴趣的人来说:

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

  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并查看它是否使用了您设置的端口号

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

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:

  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

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


回答 11

我在同一个问题上挣扎,找到了一个解决方案。我想它可以帮助您。运行时python manage.py runserver,它将在默认ip地址中使用127.0.0.1,在python环境中配置默认​​端口8000。在您的python设置中,转到<your python env>\Lib\site-packages\django\core\management\commands\runserver.py并设置1。2 default_port = '<your_port>'
.在def handle下找到并设置
if not options.get('addrport'): self.addr = '0.0.0.0' self.port = self.default_port

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

享受编码…..

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

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

Enjoy coding …..


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。