自定义django-admin命令 - AttributeError:'Command'对象没有属性'stdout' [英] custom django-admin commands - AttributeError: 'Command' object has no attribute 'stdout'
问题描述
按照编写自定义django-admin命令的示例 here ,我创建了以下自定义命令:
Following the example of writing a custom django-admin command here, I've created the following custom command:
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
args = ''
help = 'Test command'
def handle(self, *args, **options):
self.stdout.write("Hello World!")
令人惊讶的是,我收到以下堆栈跟踪:
Surprisingly, I receive the following stack trace:
Traceback (most recent call last):
File "D:\My Documents\Dev\MyProject\svn\trunk\dj_project\manage.py", line 11, in <module>
execute_manager(settings)
File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager
utility.execute()
File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python26\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python26\lib\site-packages\django\core\management\base.py", line 218, in execute
output = self.handle(*args, **options)
File "D:\My Documents\Dev\MyProject\svn\trunk\dj_project\..\dj_project\dj_app\management\commands\mytest.py", line 8, in handle
self.stdout.write("Hello World!")
AttributeError: 'Command' object has no attribute 'stdout'
怎么来的?这是一个非常基本的自定义命令,据我所知,符合示例。
How come? This is a very basic custom command that as far as I understand conforms to the example.
我正在使用django 1.2.1
I'm using django 1.2.1
推荐答案
看起来像映射到 self.stdout
是Django的中继版本的一个非常新的变化,承诺在可能。如果您正在运行1.2版本或更早版本,这将无法正常运行,您应该使用以前的文档。
It looks like the mapping to self.stdout
is a very new change in Django's trunk version, committed in May. If you're running the 1.2 release or earlier, this won't work - and you should be using the earlier documentation.
这篇关于自定义django-admin命令 - AttributeError:'Command'对象没有属性'stdout'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!