manage.py runserver
외부에서 지정 가능한 수신 대기 하는 기본 포트를 만들고 싶습니다 config.ini
. sys.argv
내부를 구문 분석 manage.py
하고 구성된 포트를 삽입하는 것보다 쉬운 수정이 있습니까?
목표는 ./manage.py runserver
매번 주소와 포트를 지정할 필요없이 config.ini
.
답변
다음을 사용하여 bash 스크립트를 만듭니다.
#!/bin/bash
exec ./manage.py runserver 0.0.0.0:<your_port>
manage.py와 동일한 디렉토리에 runserver로 저장하십시오.
chmod +x runserver
다음과 같이 실행하십시오.
./runserver
답변
실제로 개발 Django 서버에서 포트를 변경하는 가장 쉬운 방법은 다음과 같습니다.
python manage.py runserver 7000
http://127.0.0.1:7000/ 에서 개발 서버를 실행해야합니다.
답변
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)
답변
다음 명령은 모두 django를 실행하는 동안 포트를 변경할 수 있습니다.
python manage.py runserver 127.0.0.1:7000
python manage.py runserver 7000
python manage.py runserver 0:7000
답변
멤버 의 하위 클래스를 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
여기에 기본 포트 양식 설정 (다른 구성 파일을 읽음)을로드하고 있지만 다른 파일에서 직접 읽을 수도 있습니다.
답변
우리는 새로운 ‘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
![](http://daplus.net/wp-content/uploads/2023/04/coupang_part-e1630022808943-2.png)