在Django runserver命令之前运行代码一次

时间:2018-06-04 18:17:09

标签: django

要清楚,每次django服务器重新加载时我都不会说一次。

我知道可以使用AppConfig.ready()方法

来完成

我尝试重写管理命令,但即使这样也行不通

from django.core.management.commands import runserver


class Command(runserver.Command):
    def handle(self, *fixture_labels, **options):
        print('I want this to get printed exactly once.')

        super().handle(*fixture_labels, **options)

输出 -

$ python manage.py runserver
I want this to get printed exactly once.

I want this to get printed exactly once.
Performing system checks...

System check identified no issues (1 silenced).
June 04, 2018 - 23:54:55
Django version 2.0.5, using settings 'quiz.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

I want this to get printed exactly once.
Performing system checks...

System check identified no issues (1 silenced).
June 04, 2018 - 23:55:02
Django version 2.0.5, using settings 'quiz.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

还有上述代码的另一个迭代涉及使用call_command来调用runserver,但即使这样也无法完成任务。

WHY吗

我想在runserver命令之前启动一个webpack服务器,并且exaclty启动一次,这意味着每次服务器重新加载时都不会运行

1 个答案:

答案 0 :(得分:0)

每次都会重新执行manage.py脚本。您可以运行添加到shell脚本并同时启动它们。