繼編寫定製Django的管理命令here的例子中,我創建了以下自定義命令:定製Django的管理命令 - AttributeError的:「命令」對象有沒有屬性「標準輸出」
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!")
令人驚訝的是,我收到以下堆棧跟蹤:
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'
怎麼回事?這是一個非常基本的自定義命令,據我所知符合示例。
我使用Django 1.2.1
我正在運行django 1.2.1,所以仍然需要一個答案。將更新問題。 – Jonathan 2010-07-02 19:00:09
我不知道爲什麼我的答案不令人滿意。 Django 1.2.1於5月24日發佈,此更改於6月6日發佈。 – 2010-07-02 19:22:43
@Jonathan:將「self.stdout.write」替換爲「print」。 @丹尼爾我認爲你的答案會從提到實際修復中受益。但+1,因爲它幫助我解決了我的問題:) – 2010-10-15 16:29:57