• <tfoot id='G5ShV'></tfoot>
      • <bdo id='G5ShV'></bdo><ul id='G5ShV'></ul>

    1. <legend id='G5ShV'><style id='G5ShV'><dir id='G5ShV'><q id='G5ShV'></q></dir></style></legend>

        <i id='G5ShV'><tr id='G5ShV'><dt id='G5ShV'><q id='G5ShV'><span id='G5ShV'><b id='G5ShV'><form id='G5ShV'><ins id='G5ShV'></ins><ul id='G5ShV'></ul><sub id='G5ShV'></sub></form><legend id='G5ShV'></legend><bdo id='G5ShV'><pre id='G5ShV'><center id='G5ShV'></center></pre></bdo></b><th id='G5ShV'></th></span></q></dt></tr></i><div id='G5ShV'><tfoot id='G5ShV'></tfoot><dl id='G5ShV'><fieldset id='G5ShV'></fieldset></dl></div>

        <small id='G5ShV'></small><noframes id='G5ShV'>

        如何使 Django 自定义管理命令参数不需要?

        How to make a Django custom management command argument not required?(如何使 Django 自定义管理命令参数不需要?)
        <tfoot id='ZrAa4'></tfoot>

            <tbody id='ZrAa4'></tbody>
            <legend id='ZrAa4'><style id='ZrAa4'><dir id='ZrAa4'><q id='ZrAa4'></q></dir></style></legend>
            1. <i id='ZrAa4'><tr id='ZrAa4'><dt id='ZrAa4'><q id='ZrAa4'><span id='ZrAa4'><b id='ZrAa4'><form id='ZrAa4'><ins id='ZrAa4'></ins><ul id='ZrAa4'></ul><sub id='ZrAa4'></sub></form><legend id='ZrAa4'></legend><bdo id='ZrAa4'><pre id='ZrAa4'><center id='ZrAa4'></center></pre></bdo></b><th id='ZrAa4'></th></span></q></dt></tr></i><div id='ZrAa4'><tfoot id='ZrAa4'></tfoot><dl id='ZrAa4'><fieldset id='ZrAa4'></fieldset></dl></div>

              <small id='ZrAa4'></small><noframes id='ZrAa4'>

                  <bdo id='ZrAa4'></bdo><ul id='ZrAa4'></ul>
                  本文介绍了如何使 Django 自定义管理命令参数不需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 django 中编写一个自定义管理命令,如下所示-

                  I am trying to write a custom management command in django like below-

                  class Command(BaseCommand):
                  
                      def add_arguments(self, parser):
                          parser.add_argument('delay', type=int)
                  
                      def handle(self, *args, **options):
                          delay = options.get('delay', None)
                          print delay
                  

                  现在,当我运行 python manage.py mycommand 12 时,它会在控制台上打印 12.这很好.

                  Now when I am running python manage.py mycommand 12 it is printing 12 on console. Which is fine.

                  现在,如果我尝试运行 python manage.py mycommand 然后我想要,该命令默认在控制台上打印 21.但它给了我这样的东西-

                  Now if I try to run python manage.py mycommand then I want that, the command prints 21 on console by default. But it is giving me something like this-

                  usage: manage.py mycommand [-h] [--version]
                                             [-v {0,1,2,3}]
                                             [--settings SETTINGS]
                                             [--pythonpath PYTHONPATH]
                                             [--traceback]
                                             [--no-color]
                                             delay
                  

                  那么现在,如果没有给出值,我应该如何使命令参数不需要"并取默认值?

                  So now, how should I make the command argument "not required" and take a default value if value is not given?

                  推荐答案

                  文档 建议:

                  对于 nargs 等于 ?* 的位置参数,当不存在命令行参数时使用 default 值.

                  For positional arguments with nargs equal to ? or *, the default value is used when no command-line argument was present.

                  所以下面应该可以解决问题(如果提供,它将返回值,否则返回默认值):

                  So following should do the trick (it will return value if provided or default value otherwise):

                  parser.add_argument('delay', type=int, nargs='?', default=21)
                  

                  用法:

                  $ ./manage.py mycommand
                  21
                  $ ./manage.py mycommand 4
                  4
                  

                  这篇关于如何使 Django 自定义管理命令参数不需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                  <i id='0JhNW'><tr id='0JhNW'><dt id='0JhNW'><q id='0JhNW'><span id='0JhNW'><b id='0JhNW'><form id='0JhNW'><ins id='0JhNW'></ins><ul id='0JhNW'></ul><sub id='0JhNW'></sub></form><legend id='0JhNW'></legend><bdo id='0JhNW'><pre id='0JhNW'><center id='0JhNW'></center></pre></bdo></b><th id='0JhNW'></th></span></q></dt></tr></i><div id='0JhNW'><tfoot id='0JhNW'></tfoot><dl id='0JhNW'><fieldset id='0JhNW'></fieldset></dl></div>

                    <small id='0JhNW'></small><noframes id='0JhNW'>

                    <legend id='0JhNW'><style id='0JhNW'><dir id='0JhNW'><q id='0JhNW'></q></dir></style></legend>
                      <bdo id='0JhNW'></bdo><ul id='0JhNW'></ul>
                          <tbody id='0JhNW'></tbody>
                      1. <tfoot id='0JhNW'></tfoot>