在django视图上将参数传递给management.call_command [重复]

时间:2021-08-05 23:16:25

This question already has an answer here:

这个问题在这里已有答案:

I have a command i run from the command line that takes two arguments,-tzusb and -e. I have converted this so that it is called on a django view. I have a little glitch though, How do i pass this arguments to the management.call_command function I have in my views? here is my view for that

我有一个命令,我从命令行运行,带有两个参数,-tzusb和-e。我已将其转换为在django视图中调用它。我有一点小问题,如何将这些参数传递给我在视图中的management.call_command函数?这是我对此的看法

def tzusbcsv(request):
    management.call_command('artifact_db_loader','artefacts')
    return render_to_response('html/upload.html')

1 个解决方案

#1


14  

In your command you should find the option definitions which should look like the following:

在您的命令中,您应该找到选项定义,它们应如下所示:

make_option('-tzsub', dest='tzsub', action='store_true', help='Help description...')
make_option('-e', dest='e', action='store_true', help='Help description...')

Have a look on them and take into account "dest" argument for each one. Assuming you defined dest='tzsub' for -tzsub and dest='e' for -e (like in the example above), you should call the command in this way:

看看它们并考虑每个人的“dest”论点。假设您为-tzsub定义了dest ='tzsub',为-e定义了dest ='e'(如上例所示),您应该以这种方式调用该命令:

management.call_command('artifact_db_loader','artefacts', tzsub=True, e=True)

This is the same of calling the command from your console like this:

这与从控制台调用命令是一样的:

python manage.py artifact_db_loader artefacts -tzsub -e

Of course if the parameters need any arguments (so you have action='store' in the option definition) simply replace the boolean argument with the value you need. For instance:

当然,如果参数需要任何参数(因此你在选项定义中有action ='store'),只需用你需要的值替换boolean参数。例如:

management.call_command('artifact_db_loader','artefacts', tzsub='wow!', e=7)

This is the same of calling the command in this way:

这与以这种方式调用命令相同:

python manage.py artifact_db_loader artefacts -tzsub "wow!" -e 7

#1


14  

In your command you should find the option definitions which should look like the following:

在您的命令中,您应该找到选项定义,它们应如下所示:

make_option('-tzsub', dest='tzsub', action='store_true', help='Help description...')
make_option('-e', dest='e', action='store_true', help='Help description...')

Have a look on them and take into account "dest" argument for each one. Assuming you defined dest='tzsub' for -tzsub and dest='e' for -e (like in the example above), you should call the command in this way:

看看它们并考虑每个人的“dest”论点。假设您为-tzsub定义了dest ='tzsub',为-e定义了dest ='e'(如上例所示),您应该以这种方式调用该命令:

management.call_command('artifact_db_loader','artefacts', tzsub=True, e=True)

This is the same of calling the command from your console like this:

这与从控制台调用命令是一样的:

python manage.py artifact_db_loader artefacts -tzsub -e

Of course if the parameters need any arguments (so you have action='store' in the option definition) simply replace the boolean argument with the value you need. For instance:

当然,如果参数需要任何参数(因此你在选项定义中有action ='store'),只需用你需要的值替换boolean参数。例如:

management.call_command('artifact_db_loader','artefacts', tzsub='wow!', e=7)

This is the same of calling the command in this way:

这与以这种方式调用命令相同:

python manage.py artifact_db_loader artefacts -tzsub "wow!" -e 7