使用自定义设置的命令中的Django Runserver
问题描述:
我尝试在具有自定义设置的命令中执行runserver.但是未加载设置:
I try to execute the runserver inside a command with custom settings. But the settings are not loaded:
from django.core.management.base import BaseCommand
from django.core.management import call_command
from django.core.management import setup_environ, ManagementUtility
import settings_api
setup_environ(settings_api)
class Command(BaseCommand):
help = "Local API to retrieve realtime quotes"
def handle(self, *args, **options):
if not settings_api.DEBUG:
call_command('runserver', '127.0.0.1:8002')
else:
call_command('runserver', '0.0.0.0:8002')
输出为:
Used API settings
Used API settings
Validating models...
0 errors found
Django version 1.4b1, using settings 'site.settings'
Development server is running at http://0.0.0.0:8002/
Quit the server with CONTROL-C.
答
runserver
强制默认设置,除非通过-settings =
.如果需要自定义设置,请使用单独的WSGI容器(甚至是内置的WSGI容器).
runserver
forces default settings unless passed --settings=
. Use a separate WSGI container (or even the built-in one, brought up yourself) if you want custom settings.