问题描述
我正在使用Django 1.10.4和Python 3.52.当我尝试通过 python manage.py my_command
运行Django命令时,出现以下错误:
I'm using Django 1.10.4 and Python 3.52. When I try to run a Django command via python manage.py my_command
I get the following error:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "path_to_envs/envs/env_name/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
TypeError: handle() got an unexpected keyword argument 'verbosity'
我可以运行本地django服务器并与管理页面进行交互.包含该命令的应用程序位于我的 settings.py
文件中.
I can run a local django server and interact with the admin pages. The app that contains that command is in my settings.py
file.
以下是django命令的内容:
Below is the contents of the django command:
from django.core.management import BaseCommand
from my_module import MyClass
class Command(BaseCommand):
def handle(self):
my_class = MyClass()
my_class.my_method()
发生错误时, options
字典包含 {'verbosity':1,'no_color':False,'settings':None,'pythonpath':None,'traceback':False}
.根据字典 no_color
, traceback
的随机顺序,其他字典将抛出相同的 TypeError
.经过一天的搜索,我仍然无法弄清楚问题出在哪里.有人看过吗?
At the time of error, the options
dictionary contains {'verbosity': 1, 'no_color': False, 'settings': None, 'pythonpath': None, 'traceback': False}
. Depending on the random ordering of the dictionary no_color
, traceback
, and the others will throw the same TypeError
. After a day of googling I still can't figure out what the issue is. Has anyone seen this before?
推荐答案
经过大量的搜索并将我的头发拔出后,问题出在 handle()
的参数数量不正确.
After lots of googling and pulling my hair out, the issue was an incorrect number of arguments to handle()
.
此:
def handle(self):
应该是:
def handle(self, *args, **options):
这篇关于Django命令抛出TypeError:handle()得到了意外的关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!