gpt4 book ai didi

Django 管理命令看不到参数?

转载 作者:行者123 更新时间:2023-12-03 10:24:00 25 4
gpt4 key购买 nike

自从升级到 Django 1.8 后,我的 Django 管理命令中出现了一个奇怪的错误。

我运行如下:

python manage.py my_command $DB_NAME $DB_USER $DB_PASS

然后我收集参数如下:

class Command(BaseCommand):

def handle(self, *args, **options):
print args
db_name = args[0]
db_user = args[1]
db_pass = args[2]
self.conn = psycopg2.connect(database=db_name, user=db_user,
password=db_pass)

以前这工作正常,但现在我看到这个错误:

usage: manage.py my_command [-h] [--version] [-v {0,1,2,3}]
[--settings SETTINGS]
[--pythonpath PYTHONPATH]
[--traceback] [--no-color]
manage.py my_command: error: unrecognized arguments: test test test

它甚至没有达到 print args 语句的程度。

如果我在没有任何参数的情况下运行它,那么它会在 args[0] 行出错,这并不奇怪。

我在这里使用 args 错了吗?还是发生了其他事情?

最佳答案

这是 Django 1.8 中的一个变化。详细here :

Management commands that only accept positional arguments

If you have written a custom management command that only accepts positional arguments and you didn’t specify the args command variable, you might get an error like Error: unrecognized arguments: ..., as variable parsing is now based on argparse which doesn’t implicitly accept positional arguments. You can make your command backwards compatible by simply setting the args class variable. However, if you don’t have to keep compatibility with older Django versions, it’s better to implement the new add_arguments() method as described in Writing custom django-admin commands.

关于Django 管理命令看不到参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30244288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com