runserver.py 1.3 KB

123456789101112131415161718192021222324252627282930
  1. from optparse import make_option
  2. from django.conf import settings
  3. from django.core.management.commands.runserver import Command as RunserverCommand
  4. from django.contrib.staticfiles.handlers import StaticFilesHandler
  5. class Command(RunserverCommand):
  6. option_list = RunserverCommand.option_list + (
  7. make_option('--nostatic', action="store_false", dest='use_static_handler', default=True,
  8. help='Tells Django to NOT automatically serve static files at STATIC_URL.'),
  9. make_option('--insecure', action="store_true", dest='insecure_serving', default=False,
  10. help='Allows serving static files even if DEBUG is False.'),
  11. )
  12. help = "Starts a lightweight Web server for development and also serves static files."
  13. def get_handler(self, *args, **options):
  14. """
  15. Returns the static files serving handler wrapping the default handler,
  16. if static files should be served. Otherwise just returns the default
  17. handler.
  18. """
  19. handler = super(Command, self).get_handler(*args, **options)
  20. use_static_handler = options.get('use_static_handler', True)
  21. insecure_serving = options.get('insecure_serving', False)
  22. if use_static_handler and (settings.DEBUG or insecure_serving):
  23. return StaticFilesHandler(handler)
  24. return handler