Ticket #10435: runserver-custom-handler.diff

File runserver-custom-handler.diff, 2.9 KB (added by Trevor Caira, 15 years ago)
  • django/core/management/commands/runserver.py

     
    33import os
    44import sys
    55
     6def load_wsgi_handler(dotted_path):
     7    try:
     8        dot = dotted_path.rindex('.')
     9    except ValueError:
     10        raise ValueError, "%r isn't a WSGI handler class" % dotted_path
     11    handler_module, handler_classname = dotted_path[:dot], \
     12                                        dotted_path[dot+1:]
     13    __import__(handler_module, {}, {}, [])
     14    mod = sys.modules[handler_module]
     15    try:
     16        return getattr(mod, handler_classname)
     17    except AttributeError:
     18        raise ValueError, 'Handler module "%s" does not define a "%s" \
     19class' % (handler_module, handler_classname)
     20
    621class Command(BaseCommand):
    722    option_list = BaseCommand.option_list + (
    823        make_option('--noreload', action='store_false', dest='use_reloader', default=True,
    924            help='Tells Django to NOT use the auto-reloader.'),
    1025        make_option('--adminmedia', dest='admin_media_path', default='',
    1126            help='Specifies the directory from which to serve admin media.'),
     27        make_option('--handler', dest='wsgihandler',
     28                    default='django.core.handlers.wsgi.WSGIHandler',
     29                    help='Specify an alterate WSGI handler class'),
    1230    )
    1331    help = "Starts a lightweight Web server for development."
    1432    args = '[optional port number, or ipaddr:port]'
     
    1937    def handle(self, addrport='', *args, **options):
    2038        import django
    2139        from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
    22         from django.core.handlers.wsgi import WSGIHandler
     40        WSGIHandler = load_wsgi_handler(options['wsgihandler'])
    2341        if args:
    2442            raise CommandError('Usage is runserver %s' % self.args)
    2543        if not addrport:
  • docs/ref/django-admin.txt

     
    502502
    503503    django-admin.py runserver --noreload
    504504
     505--handler
     506~~~~~~~~~
     507
     508Use the ``--handler`` option to specify an alternate WSGI handler class.
     509This allows you to use the development server with a custom handler
     510subclass instead of the default
     511``django.core.handlers.wsgi.WSGIHandler``. Overriding the WSGI handler
     512is useful in case you want to override any aspect of request handling,
     513such as customizing 500 error handling. This option should be passed a
     514dotted path to a ``WSGIHandler`` subclass.
     515
     516Example usage::
     517
     518    django-admin.py runserver --handler=wsgi.handler.WSGIHandler
     519
    505520Examples of using different ports and addresses
    506521~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    507522
Back to Top