Python 扩展django';s`runserver`命令添加参数
为了尝试通用化django应用程序,以便它可以使用Python 扩展django';s`runserver`命令添加参数,python,django,python-3.x,django-commands,Python,Django,Python 3.x,Django Commands,为了尝试通用化django应用程序,以便它可以使用yaml文件中定义的不同配置运行,我想扩展runserver命令,将这些配置文件的位置作为参数 我照做了,但是当我尝试使用我的“添加”参数运行命令时,它似乎不起作用 我有一种感觉,这是因为管理/commands目录的位置,但移动它(使用\uuu init\uuuuuuuuuuuupy并没有帮助) 有什么建议吗?谢谢 我的应用程序结构 my_app/ __init__.py static/ templates/ ma
yaml
文件中定义的不同配置运行,我想扩展runserver
命令,将这些配置文件的位置作为参数
我照做了,但是当我尝试使用我的“添加”参数运行命令时,它似乎不起作用
我有一种感觉,这是因为管理/commands
目录的位置,但移动它(使用\uuu init\uuuuuuuuuuuupy
并没有帮助)
有什么建议吗?谢谢
我的应用程序结构
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
发生了什么错误?请确保您已将
myapp
添加到已安装的应用程序
。此外,我没有在您的myapp
文件夹中看到\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuupy
。@diegupuente这是一个错误。那里有一个\uuuu init\uuuu.py
。更新.Django只会在INSTALLED\u apps
设置中检查应用的管理命令(以及模型、模板标记等)。如果您不想将myapp
添加到INSTALLED\u APPS
,因为它是您的项目目录,那么您可以创建一个新的应用程序并将管理目录移动到那里。