./manage.py runserver (--option)← Add options here
-I want to add new processing while keeping the existing operation in runserver
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 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',
]
・ 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