How to add options to Django's manage.py runserver

What I tried to do

./manage.py runserver (--option)← Add options here

-I want to add new processing while keeping the existing operation in runserver

Override Command

app name/management/commands/runserver.py



from django.contrib.staticfiles.management.commands.runserver import Command as RunCommand

class Command(RunCommand):
       
       help = "Command itself(runserver)Explanation etc."

       def add_arguments(self, parser):
           super().add_arguments(parser)
        
           parser.add_argument(
            'option name',    help='--Sentence next to option at help'
        )
      
      def handle(self, *args, **options):

          #Process here

          super().handle(*args, **options)

-In the argument of * parser.add_argument *, you can set whether the command option to be created requires an argument. ・ There are other items that can be set with arguments.

It is not recognized even if I type a command (it does not appear in --help)

solution

-It seems that the app created here must be higher than the app overridden by INSTALLED_APPS in settings. (See [here] for details (https://docs.djangoproject.com/en/2.2/howto/custom-management-commands/#overriding-commands))

INSTALLED_APPS = [
    'app', #Change
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Came out

・ This time, add with --example


 ./manage.py runserver --help
usage: manage.py runserver [-h] [--ipv6] [--nothreading] [--noreload]
                           [--nostatic] [--insecure] [--example] [--version]
                           [-v {0,1,2,3}] [--settings SETTINGS]
                           [--pythonpath PYTHONPATH] [--traceback]
                           [--no-color] [--force-color]
                           [addrport]

Starts a lightweight Web server for development and also serves static files.

positional arguments:
  addrport              Optional port number, or ipaddr:port

optional arguments:
  -h, --help            show this help message and exit
  --ipv6, -6            Tells Django to use an IPv6 address.
  --nothreading         Tells Django to NOT use threading.
  --noreload            Tells Django to NOT use the auto-reloader.
  --nostatic            Tells Django to NOT automatically serve static files
                        at STATIC_URL.
  --insecure            Allows serving static files even if DEBUG is False.
  --example             #This is the option added this time
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
  --force-color         Force colorization of the command output.

Recommended Posts

How to add options to Django's manage.py runserver
[Python] [Django] How to use ChoiceField and how to add options
How to use Django's GeoIp2
How to add sudo when debugging
Add parameters to Django's custom commands
How to add AWS EBS volume
How to add a package with PyCharm
How to use ManyToManyField with Django's Admin
How to add python module to anaconda environment
How to view images in Django's Admin
How to add arbitrary headers to response with FastAPI
How to add a Python module search path
How to add Anaconda Powershell Prompt to Windows Terminal?
[ESXi (vCenter)] How to add NIC for CentOS 7.3
How to specify a schema in Django's database settings
How to add help to HDA (with Python script bonus)
How to get a logged-in user with Django's forms.py
How to deal with Django's Template Does Not Exist
How to use the exists clause in Django's queryset
How to add page numbers to PDF files (in Python)
[Morphological analysis] How to add a new dictionary to Mecab
How to add color bars in matplotlib bar plot
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to update Spyder
How to use IPython
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to grow dotfiles
How to use list []
How to use python-kabusapi
"How to count Fukashigi"
How to install Nbextensions
How to use OptParse
How to use return