有一种方法可以在现有的django命令中添加功能? [英] There is a way to add features to an existing django command?
问题描述
我想在django命令启动之前运行命令。
I want to run a command just before the a django command is started.
例如:
$ python manage.py runserver
Validating models...
0 errors found
Django version 1.3, using settings 'creat1va.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
(started some command in the background)
[10/Jul/2011 21:50:26] "GET / HTTP/1.1" 200 1611
[10/Jul/2011 21:50:26] "GET /assets/css/master.css HTTP/1.1" 404 1783
[10/Jul/2011 21:50:26] "GET /assets/images/misc/logo.png HTTP/1.1" 404 1801
[10/Jul/2011 21:50:26] "GET /assets/images/icons/bo.gif HTTP/1.1" 404 1798
[10/Jul/2011 21:50:28] (My background process) "Some nice Feedback"
主要的想法是开始一个背景处理并输出日志记录。
The main idea is to start a background process, and output the logging.
有没有办法实现这个,而不是黑客的django源?
Is there a way to achieve this, without hacking the django sources?
推荐答案
只要知道你可以轻松地覆盖命令,使用同一个命令的应用程序。
Just realize that you can override the commands just easily as making an app with a command with the same name.
所以我创建一个应用程序并创建与runserver名称相同的文件,稍后再扩展runserver基类以在运行之前添加一个新功能。
So I create an app and create a file with the same name as runserver, and later on that extend the runserver base class to add a new feature before it runs.
例如,我想运行命令$ c ompass手表,在runserver启动之前,并保持运行在runserver执行。
For example, I want to run the command $ compass watch, just before runserver starts and keep it running along runserver execution.
"""
Start $compass watch, command when you do $python manage.py runserver
file: main/management/commands/runserver.py
Add ´main´ app to the last of the installed apps
"""
from optparse import make_option
import os
import subprocess
from django.core.management.base import BaseCommand, CommandError
from django.core.management.commands.runserver import BaseRunserverCommand
from django.conf import settings
class 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 %r\n" % 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 %r\n" % self.compass_pid.pid)
super(Command, self).inner_run(*args, **options)
这个工作很好。
查看 https://docs.djangoproject.com/en/dev/ howto / custom-management-commands / 进一步了解ls about django命令
Look at https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ for more details about django commands
希望有人找到有用的
这篇关于有一种方法可以在现有的django命令中添加功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!