Django

Code

root/django/trunk/django/core/management/commands/runserver.py

Revision 8749, 3.6 kB (checked in by mtredinnick, 10 months ago)

Fixed #8702 -- Set up the initial locale correctly for the development server.
Previously, "--noreload" wasn't picking up the default language setting. Thanks
to arien and Karen Tracey for debugging this.

  • Property svn:eol-style set to native
Line 
1 from django.core.management.base import BaseCommand, CommandError
2 from optparse import make_option
3 import os
4 import sys
5
6 class Command(BaseCommand):
7     option_list = BaseCommand.option_list + (
8         make_option('--noreload', action='store_false', dest='use_reloader', default=True,
9             help='Tells Django to NOT use the auto-reloader.'),
10         make_option('--adminmedia', dest='admin_media_path', default='',
11             help='Specifies the directory from which to serve admin media.'),
12     )
13     help = "Starts a lightweight Web server for development."
14     args = '[optional port number, or ipaddr:port]'
15
16     # Validation is called explicitly each time the server is reloaded.
17     requires_model_validation = False
18
19     def handle(self, addrport='', *args, **options):
20         import django
21         from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
22         from django.core.handlers.wsgi import WSGIHandler
23         if args:
24             raise CommandError('Usage is runserver %s' % self.args)
25         if not addrport:
26             addr = ''
27             port = '8000'
28         else:
29             try:
30                 addr, port = addrport.split(':')
31             except ValueError:
32                 addr, port = '', addrport
33         if not addr:
34             addr = '127.0.0.1'
35
36         if not port.isdigit():
37             raise CommandError("%r is not a valid port number." % port)
38
39         use_reloader = options.get('use_reloader', True)
40         admin_media_path = options.get('admin_media_path', '')
41         shutdown_message = options.get('shutdown_message', '')
42         quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
43
44         def inner_run():
45             from django.conf import settings
46             from django.utils import translation
47             print "Validating models..."
48             self.validate(display_num_errors=True)
49             print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
50             print "Development server is running at http://%s:%s/" % (addr, port)
51             print "Quit the server with %s." % quit_command
52
53             # django.core.management.base forces the locale to en-us. We should
54             # set it up correctly for the first request (particularly important
55             # in the "--noreload" case).
56             translation.activate(settings.LANGUAGE_CODE)
57
58             try:
59                 path = admin_media_path or django.__path__[0] + '/contrib/admin/media'
60                 handler = AdminMediaHandler(WSGIHandler(), path)
61                 run(addr, int(port), handler)
62             except WSGIServerException, e:
63                 # Use helpful error messages instead of ugly tracebacks.
64                 ERRORS = {
65                     13: "You don't have permission to access that port.",
66                     98: "That port is already in use.",
67                     99: "That IP address can't be assigned-to.",
68                 }
69                 try:
70                     error_text = ERRORS[e.args[0].args[0]]
71                 except (AttributeError, KeyError):
72                     error_text = str(e)
73                 sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n')
74                 # Need to use an OS exit because sys.exit doesn't work in a thread
75                 os._exit(1)
76             except KeyboardInterrupt:
77                 if shutdown_message:
78                     print shutdown_message
79                 sys.exit(0)
80
81         if use_reloader:
82             from django.utils import autoreload
83             autoreload.main(inner_run)
84         else:
85             inner_run()
Note: See TracBrowser for help on using the browser.