为了尝试概括django应用程序,因此它可以使用yaml
文件中定义的不同配置运行,我想扩展runserver
命令以获取这些配置文件的位置作为论点。
我跟着a guide by Chase Seibert,但是当我尝试使用“添加”参数运行命令时,它似乎不起作用。
我感觉这是因为management/commands
目录的位置,但移动它(使用__init__.py
没有帮助)。
有什么建议吗?谢谢!
我的应用结构
my_app/
__init__.py
static/
templates/
manage.py
app1/
app2/
my_app/
__init__.py
management/
__init__.py
commands/
__init__.py
runserver.py
models.py
views.py
settings.py
urls.py
我的runserver
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.core.management.commands.runserver import BaseRunserverCommand
from .env import PATH_CFG_SIM, PATH_CFG_PARAM
class Command(BaseCommand):
''' Runs the built-in Django runserver with initial schema and fixtures
Note: cannot use auto-reloading because that casues resetdb to be called
twice.
'''
# remove this option from the --help for this command
option_list = (o for o in BaseRunserverCommand.option_list if o.dest != 'use_reloader')
help = 'Starts a lightweight web server testing.'
args = '[optional port number, or ipaddr:port] appname appname'
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument('--cfg-sim', action='store',
dest='path_cfg_sim',
help='Set path to simulation.yml configurations')
parser.add_argument('--cfg-param', action='store',
dest='path_cfg_param',
help='Set path to parameters.yml configurations')
def handle(self, addrport='', *args, **options):
# Set path parameters if passed
if options['path_cfg_sim']:
PATH_CFG_SIM = options['path_cfg_sim']
if options['path_cfg_param']:
PATH_CFG_PARAM = options['path_cfg_param']
call_command('syncdb', *args, **options)
# BUG: runserver gets called twice if --noreload is False
# https://code.djangoproject.com/ticket/8085
options['use_reloader'] = False
call_command('runserver', addrport, *args, **options)
return None