@@ -43,25 +43,44 @@ def __init__(self, *args, **kwargs):
43
43
httpd .serve_forever ()
44
44
45
45
46
+ ADDITIONAL_ARGUMENTS = {
47
+ '--werkzeug' : dict (
48
+ action = 'store_true' ,
49
+ dest = 'use_werkzeug' ,
50
+ default = False ,
51
+ help = 'Tells Django to use the Werkzeug interactive debugger.' ),
52
+ '--forked' : dict (
53
+ action = 'store_true' ,
54
+ dest = 'use_forked' ,
55
+ default = False ,
56
+ help = 'Use forking instead of threading for multiple web requests.' ),
57
+ '--dozer' : dict (
58
+ action = 'store_true' ,
59
+ dest = 'use_dozer' ,
60
+ default = False ,
61
+ help = 'Enable the Dozer memory debugging middleware.' ),
62
+ '--wsgi-app' : dict (
63
+ dest = 'wsgi_app' ,
64
+ default = None ,
65
+ help = 'Load the specified WSGI app as the server endpoint.' ),
66
+ }
67
+
68
+ if any (map (lambda app : app in settings .INSTALLED_APPS , STATICFILES_APPS )):
69
+ ADDITIONAL_ARGUMENTS .update ({
70
+ '--nostatic' : dict (
71
+ dest = 'use_static_files' ,
72
+ action = 'store_false' ,
73
+ default = True ,
74
+ help = 'Tells Django to NOT automatically serve static files at STATIC_URL.' )
75
+ })
76
+
46
77
class Command (BaseCommand ):
47
- option_list = BaseCommand .option_list + (
48
- make_option (
49
- '--werkzeug' , action = 'store_true' , dest = 'use_werkzeug' , default = False ,
50
- help = 'Tells Django to use the Werkzeug interactive debugger.' ),
51
- make_option (
52
- '--forked' , action = 'store_true' , dest = 'use_forked' , default = False ,
53
- help = 'Use forking instead of threading for multiple web requests.' ),
54
- make_option (
55
- '--dozer' , action = 'store_true' , dest = 'use_dozer' , default = False ,
56
- help = 'Enable the Dozer memory debugging middleware.' ),
57
- make_option (
58
- '--wsgi-app' , dest = 'wsgi_app' , default = None ,
59
- help = 'Load the specified WSGI app as the server endpoint.' ),
60
- )
61
- if any (map (lambda app : app in settings .INSTALLED_APPS , STATICFILES_APPS )):
62
- option_list += make_option (
63
- '--nostatic' , dest = 'use_static_files' , action = 'store_false' , default = True ,
64
- help = 'Tells Django to NOT automatically serve static files at STATIC_URL.' ),
78
+ if BaseCommand .option_list :
79
+ # Handle Django < 1.8
80
+ option_list = BaseCommand .option_list + (
81
+ make_option (name , ** kwargs )
82
+ for name , kwargs in ADDITIONAL_ARGUMENTS .items ()
83
+ )
65
84
66
85
help = "Starts a lightweight Web server for development which outputs additional debug information."
67
86
args = '[optional port number, or ipaddr:port]'
@@ -77,6 +96,11 @@ def __init__(self):
77
96
requires_model_validation = False # Django < 1.7
78
97
super (Command , self ).__init__ ()
79
98
99
+ def add_arguments (self , parser ):
100
+ super (Command , self ).add_arguments (parser )
101
+ for name , kwargs in ADDITIONAL_ARGUMENTS .items ():
102
+ parser .add_argument (name , ** kwargs )
103
+
80
104
def run_from_argv (self , argv ):
81
105
parser = self .create_parser (argv [0 ], argv [1 ])
82
106
default_args = getattr (settings , 'DEVSERVER_ARGS' , None )
@@ -85,12 +109,24 @@ def run_from_argv(self, argv):
85
109
else :
86
110
options = None
87
111
88
- options , args = parser .parse_args (argv [2 :], options )
112
+ if getattr (self , 'use_argparse' , False ):
113
+ options = parser .parse_args (argv [2 :], options )
114
+ cmd_options = vars (options )
115
+ args = cmd_options .pop ('args' , ())
116
+ else :
117
+ options , args = parser .parse_args (argv [2 :], options )
118
+ cmd_options = var (options )
89
119
90
120
handle_default_options (options )
91
121
self .execute (* args , ** options .__dict__ )
92
122
93
- def handle (self , addrport = '' , * args , ** options ):
123
+ def handle (self , * args , ** options ):
124
+ options .pop ('addrport' , None )
125
+ if args :
126
+ addrport , args = args [0 ], args [1 :]
127
+ else :
128
+ addrport , args = '' , args
129
+
94
130
if args :
95
131
raise CommandError ('Usage is runserver %s' % self .args )
96
132
0 commit comments