栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有没有一种方法可以向现有的django命令添加功能?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有没有一种方法可以向现有的django命令添加功能?

只需意识到您可以轻松地覆盖命令,就像使用具有相同名称的命令制作应用程序一样。

因此,我创建了一个应用程序,并创建了一个与runserver相同名称的文件,然后扩展了runserver基类以在运行之前添加新功能。

例如,我想在runserver启动之前运行$ $ watch watch命令,并使其在runserver执行期间一直运行。

"""Start $compass watch, command when you do $python manage.py runserverfile: main/management/commands/runserver.pyAdd ´main´ app to the last of the installed apps"""from optparse import make_optionimport osimport subprocessfrom django.core.management.base import baseCommand, CommandErrorfrom django.core.management.commands.runserver import baseRunserverCommandfrom django.conf import settingsclass Command(baseRunserverCommand):    option_list = baseRunserverCommand.option_list + (        make_option('--adminmedia', dest='admin_media_path', default='', help='Specifies the directory from which to serve admin media.'),        make_option('--watch', dest='compass_project_path', default=settings.MEDIA_ROOT, help='Specifies the project directory for compass.'),    )    def inner_run(self, *args, **options):        self.compass_project_path = options.get('compass_project_path', settings.MEDIA_ROOT)        self.stdout.write("Starting the compass watch command for %rn" % self.compass_project_path)        self.compass_pid = subprocess.Popen(["compass watch %s" % self.compass_project_path], shell=True, stdin=subprocess.PIPE, stdout=self.stdout, stderr=self.stderr)        self.stdout.write("Compas watch process on %rn" % self.compass_pid.pid)        super(Command, self).inner_run(*args, **options)

这样很好。

查看https://docs.djangoproject.com/en/dev/howto/custom-management-
commands/
了解有关django命令的更多详细信息

希望有人觉得这有帮助



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号