gpt4 book ai didi

django change default runserver port(Django更改默认运行服务器端口)

转载 作者:bug小助手 更新时间:2023-10-25 11:44:15 25 4
gpt4 key购买 nike



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?

我希望在一个无关的config.ini中指定made.py runserver监听的默认端口。有没有比在made.py中解析sys.argv并插入已配置的端口更容易的修复方法?



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.

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


更多回答

If you run the command from your command line history, then it's even fewer keystrokes. E.g. for me it's "<ctrl-r>runs<enter>", and it automatically runs the same command as last time, so the port etc is included.

如果从命令行历史记录运行该命令,则击键次数甚至会更少。例如,对我来说,它是“run”,它会自动运行与上次相同的命令,因此包括端口等。

优秀答案推荐

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


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/上运行开发服务器



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:

在Django 1.9版本中,我找到的最简单的解决方案(基于Quentin Stafford-Fraser的解决方案)是在made.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)


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


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:

创建django.core.management.commands.runserver.Command的子类并覆盖DEFAULT_PORT成员。将该文件另存为您自己的管理命令,例如保存在 /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.

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



you can try to add an argument in manage.py like this

您可以尝试将参数添加到Manage.py中,如下所示


python manage.py runserver 0.0.0.0:5000 


python manage.py runserver <your IP>:<port>



or you pass the port like this

或者你像这样通过港口


python manage.py runserver 5000


python manage.py runserver <your port>




in the last version of Django(Right now: 4.0.3), you can add these lines to your settings.py file

在Django的最新版本(目前为4.0.3)中,您可以将以下几行添加到settings.py文件中



from django.core.management.commands.runserver import Command as runserver
runserver.default_port = "8000"


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:

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


In Pycharm you can simply add the port to the parameters

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



enter image description here



python manage.py runserver <your port>

after in browser run 127.0.0.1:(your port)

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



in your project manage.py file add

在您的项目manage.py文件中添加


from django.core.management.commands.runserver import Command as runserver

then in def main():

然后在def main()中:


runserver.default_port = "8001" 


Use django-extensions (with Werkzeug) and then simply put into your settings

使用Django扩展(与Werkzeug),然后简单地放入您的设置


RUNSERVERPLUS_SERVER_ADDRESS_PORT = '0.0.0.0:6000'

Example output

输出示例


> python manage.py runserver_plus
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:6000
* Running on http://192.168.2.8:6000

You can do it also with vanilla Django with some suggestions from others. But I've found using django-extensions is a must to be productive anyway.

你也可以在其他人的建议下用香草Django做到这一点。但我发现,无论如何都必须使用Django-Expanies才能提高工作效率。



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进行调试/测试时,这才是相关的。




  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.

我正在使用zsh和Viralenvs包装器。我把出口放在项目中,后期激活脚本,并为每个项目指定端口。



workon someproject
runserver


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

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



For Django 3.x, just change default_port in settings.py. Like this:

对于Django 3.x,只需更改settings.py中的DEFAULT_PORT。就像这样:


from decouple import config
import django.core.management.commands.runserver as runserver

runserver.Command.default_port = config('WebServer_Port', default = "8088")

Then, if you want to specify the port, just add a new line in your setting.ini

然后,如果您想指定端口,只需在setting.ini中添加新行


[settings]
WebServer_Port=8091

If not, delete this parameter.

如果不是,请删除此参数。



run this command

运行此命令


python .\manage.py runserver 8080

Python.\Manage.py运行服务器8080


8080 is your port you can change it :)

8080是您的端口,您可以更改它:)



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



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

它可以在python2.7上运行,但也应该可以在较新版本的python上运行。祝好运



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

我一直在努力解决同样的问题,并找到了一个解决方案。我想它能帮到你。当你运行python manage.py runserver时,它会将127.0.0.1作为默认的ip地址,8000作为默认的端口号,可以在你的python环境中配置。在python设置中,转到 \Lib\site-packages\django\core\management\commands\runserver.py并设置1。default_port =“ ”2.在def handle下找到这个并设置if not选项。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 made.py runserver”,它将默认运行在“0.0.0.0:



Enjoy coding .....

享受编码.....


更多回答

Either that, or I am thinking about adding a custom management command.

或者,或者我正在考虑添加自定义管理命令。

You can't run the development server programmatically, so a custom command won't work unless it calls the shell using something like call. If this solution works for you please make sure to mark it as solved.

您不能以编程方式运行开发服务器,因此除非使用Call之类的命令调用外壳,否则自定义命令将不起作用。如果此解决方案对您有效,请确保将其标记为已解决。

I actually started using supervisor for this now, that makes it even easier to manage. :-) But yours is probably the cleanest solution other than writing a dedicated runserver script.

实际上,我现在开始使用Supervisor来管理它,这使得它更容易管理。:-)但您的解决方案可能是最干净的解决方案,而不是编写专用的运行服务器脚本。

Supervisor is an good solution for I wouldn't recommend it to run the development environment. You lose the advantage of having the server output on the terminal, among other things. If you really want to use supervisor my advice would be to use it with a fully featured WSGI server like Gunicorn. Please don't run the development server as your production server...

Supervisor是一个很好的解决方案,因为我不推荐它运行开发环境。您失去了在终端上输出服务器的优势,以及其他优势。如果你真的想使用Supervisor,我的建议是使用一个功能齐全的WSGI服务器,比如Gunicorn。请不要将开发服务器作为您的生产服务器运行...

This is useful but not greate when juggling multiple projects at a time - I would have accepted the answer below which specifies the port to be used for each distinct project. Just my opinion tho.

这是有用的,但不是伟大的杂耍时,多个项目在同一时间-我会接受下面的答案,其中指定的端口将用于每个不同的项目。只是我的意见。

This answer is about changing the port, not changing the default port.

这个答案是关于更改端口,而不是更改默认端口。

This should be the correct answer, accepted one also implies the IP Address, and executing manage.py for some reason, instead of calling it via python

这应该是正确的答案,接受的答案也意味着IP地址,并出于某种原因执行made.py,而不是通过python调用它

Although it doesn't answer the original question exactly, and the indenting is messed up (code should be indented from "import django" on down), I prefer this answer because it is entirely self-contained and does not require changing what is entered on the command line.

尽管它没有准确地回答原始问题,而且缩进也很混乱(代码应该从“导入Django”向下缩进),但我更喜欢这个答案,因为它是完全独立的,不需要更改在命令行中输入的内容。

@PurpleDiane The indentation is fixed.

@PurpleDiane的缩进是固定的。

My manage.py doesn't really look like that, I guess Django changed since

我的made.py看起来并不像那样,我猜Django自从

Currently (as of 2.0.3) you can just add: from django.core.management.commands.runserver import Command as runserver; runserver.default_port = "8080 to your manage.py. You can also change the listening address with: runserver.default_addr

目前(从2.0.3起),您只需将:from django.core.madement.Commands.runserver导入命令作为runserver;runserver.default_port=“8080添加到您的made.py。您还可以使用以下命令更改侦听地址:runserver.default_addr

"The goal is to run ./manage.py runserver without having to specify address and port every time"

“我们的目标是在不需要每次都指定地址和端口的情况下运行./Manage.py runserver”

This seems like the best solution, however, my Django 1.8.14 doesn't recognize my self-created file runserver.py. Should I register it somewhere?

这似乎是最好的解决方案,然而,我的Django 1.8.14不能识别我自己创建的文件runserver.py。我应该在什么地方挂号吗?

@physicalattraction Your file is probably not in the right location. docs.djangoproject.com/en/2.0/howto/custom-management-commands starts with a description of where to put the python module for the runserver command. Also maybe you have another app also registering a command named runserver. Try renaming your command and see whether it is recognized.

@物理吸引您的文件可能没有放在正确的位置。Docs.djangoproject.com/en/2.0/howto/custom-management-commands首先描述了将runserver命令的python模块放在哪里。此外,您可能还有另一个应用程序也注册了一个名为runserver的命令。尝试重命名您的命令,并查看它是否被识别。

I put it in <app_name>/management/commands/runserver.py, but then Django's original runserver is used. When I rename it to run_server.py, it is recognized. I don't see anything special about runserver on the page you link to.

我将其放在/Management/Commands/runserver.py中,但随后使用的是Django的原始runserver。当我将其重命名为run_server.py时,它被识别。在您链接的页面上,我看不到runserver有什么特别之处。

The thing is staticfiles does exactly what you suggest. So following your instructions breaks serving static files in development. It's best to import from django.contrib.staticfiles.management.commands.

问题是,静态文件完全按照您的建议执行。因此,按照您的说明在开发过程中为静态文件提供服务是有害的。最好从django.contrib.staticfiles.management.commands.导入

Yup working in 4.2.2

是的在4.2.2中工作

This works. I recommend not running on all addresses though: RUNSERVERPLUS_SERVER_ADDRESS_PORT = '127.0.0.1:6000'

这很管用。但我建议不要在所有地址上运行:RUNSERVERPLUS_SERVER_ADDRESS_PORT=‘127.0.0.1:6000’

This is the worst suggestion of all here. IMHO, editing a file from a distribution is never a good idea and leads to confusion because the change is not tracked by a VCS and gets easily overwritten.

这是这里所有建议中最糟糕的。IMHO,从发行版编辑文件从来都不是一个好主意,而且会导致混淆,因为更改不会被VCS跟踪,并且很容易被覆盖。

It's not recommended to edit the Django module at the site packages. It can be updated by new version. Also it affect to all Django apps.

不建议在站点包中编辑Django模块。可以通过新版本进行更新。它也会影响到所有的Django应用程序。

Modifying the sources of dependent packages is considered bad practice. Changes can be lost on the update / reinstalling.

修改依赖包的源代码被认为是不好的做法。更新/重新安装时更改可能会丢失。

That's right @arogachev, It is just an option by which you can make default port and host. But it's really not a good practice to modify dependent packages. you can set the IP and port when you run your server in command prompt as well.

这是对的@arogachev,这只是一个选项,您可以通过它来设置默认端口和主机。但修改依赖包确实不是一种好的做法。您也可以在命令提示符下运行服务器时设置IP和端口。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com