32 request_middleware = [] |
32 request_middleware = [] |
33 for middleware_path in settings.MIDDLEWARE_CLASSES: |
33 for middleware_path in settings.MIDDLEWARE_CLASSES: |
34 try: |
34 try: |
35 dot = middleware_path.rindex('.') |
35 dot = middleware_path.rindex('.') |
36 except ValueError: |
36 except ValueError: |
37 raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path |
37 raise exceptions.ImproperlyConfigured('%s isn\'t a middleware module' % middleware_path) |
38 mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] |
38 mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] |
39 try: |
39 try: |
40 mod = import_module(mw_module) |
40 mod = import_module(mw_module) |
41 except ImportError, e: |
41 except ImportError, e: |
42 raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e) |
42 raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e)) |
43 try: |
43 try: |
44 mw_class = getattr(mod, mw_classname) |
44 mw_class = getattr(mod, mw_classname) |
45 except AttributeError: |
45 except AttributeError: |
46 raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname) |
46 raise exceptions.ImproperlyConfigured('Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname)) |
47 |
47 |
48 try: |
48 try: |
49 mw_instance = mw_class() |
49 mw_instance = mw_class() |
50 except exceptions.MiddlewareNotUsed: |
50 except exceptions.MiddlewareNotUsed: |
51 continue |
51 continue |
66 def get_response(self, request): |
66 def get_response(self, request): |
67 "Returns an HttpResponse object for the given HttpRequest" |
67 "Returns an HttpResponse object for the given HttpRequest" |
68 from django.core import exceptions, urlresolvers |
68 from django.core import exceptions, urlresolvers |
69 from django.conf import settings |
69 from django.conf import settings |
70 |
70 |
71 # Apply request middleware |
|
72 for middleware_method in self._request_middleware: |
|
73 response = middleware_method(request) |
|
74 if response: |
|
75 return response |
|
76 |
|
77 # Get urlconf from request object, if available. Otherwise use default. |
|
78 urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) |
|
79 |
|
80 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) |
|
81 try: |
71 try: |
82 callback, callback_args, callback_kwargs = resolver.resolve( |
72 try: |
83 request.path_info) |
73 # Setup default url resolver for this thread. |
84 |
74 urlconf = settings.ROOT_URLCONF |
85 # Apply view middleware |
75 urlresolvers.set_urlconf(urlconf) |
86 for middleware_method in self._view_middleware: |
76 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) |
87 response = middleware_method(request, callback, callback_args, callback_kwargs) |
77 |
88 if response: |
78 # Apply request middleware |
89 return response |
79 for middleware_method in self._request_middleware: |
90 |
80 response = middleware_method(request) |
91 try: |
|
92 response = callback(request, *callback_args, **callback_kwargs) |
|
93 except Exception, e: |
|
94 # If the view raised an exception, run it through exception |
|
95 # middleware, and if the exception middleware returns a |
|
96 # response, use that. Otherwise, reraise the exception. |
|
97 for middleware_method in self._exception_middleware: |
|
98 response = middleware_method(request, e) |
|
99 if response: |
81 if response: |
100 return response |
82 return response |
|
83 |
|
84 if hasattr(request, "urlconf"): |
|
85 # Reset url resolver with a custom urlconf. |
|
86 urlconf = request.urlconf |
|
87 urlresolvers.set_urlconf(urlconf) |
|
88 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) |
|
89 |
|
90 callback, callback_args, callback_kwargs = resolver.resolve( |
|
91 request.path_info) |
|
92 |
|
93 # Apply view middleware |
|
94 for middleware_method in self._view_middleware: |
|
95 response = middleware_method(request, callback, callback_args, callback_kwargs) |
|
96 if response: |
|
97 return response |
|
98 |
|
99 try: |
|
100 response = callback(request, *callback_args, **callback_kwargs) |
|
101 except Exception, e: |
|
102 # If the view raised an exception, run it through exception |
|
103 # middleware, and if the exception middleware returns a |
|
104 # response, use that. Otherwise, reraise the exception. |
|
105 for middleware_method in self._exception_middleware: |
|
106 response = middleware_method(request, e) |
|
107 if response: |
|
108 return response |
|
109 raise |
|
110 |
|
111 # Complain if the view returned None (a common error). |
|
112 if response is None: |
|
113 try: |
|
114 view_name = callback.func_name # If it's a function |
|
115 except AttributeError: |
|
116 view_name = callback.__class__.__name__ + '.__call__' # If it's a class |
|
117 raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name)) |
|
118 |
|
119 return response |
|
120 except http.Http404, e: |
|
121 if settings.DEBUG: |
|
122 from django.views import debug |
|
123 return debug.technical_404_response(request, e) |
|
124 else: |
|
125 try: |
|
126 callback, param_dict = resolver.resolve404() |
|
127 return callback(request, **param_dict) |
|
128 except: |
|
129 try: |
|
130 return self.handle_uncaught_exception(request, resolver, sys.exc_info()) |
|
131 finally: |
|
132 receivers = signals.got_request_exception.send(sender=self.__class__, request=request) |
|
133 except exceptions.PermissionDenied: |
|
134 return http.HttpResponseForbidden('<h1>Permission denied</h1>') |
|
135 except SystemExit: |
|
136 # Allow sys.exit() to actually exit. See tickets #1023 and #4701 |
101 raise |
137 raise |
102 |
138 except: # Handle everything else, including SuspiciousOperation, etc. |
103 # Complain if the view returned None (a common error). |
139 # Get the exception info now, in case another exception is thrown later. |
104 if response is None: |
140 exc_info = sys.exc_info() |
105 try: |
141 receivers = signals.got_request_exception.send(sender=self.__class__, request=request) |
106 view_name = callback.func_name # If it's a function |
142 return self.handle_uncaught_exception(request, resolver, exc_info) |
107 except AttributeError: |
143 finally: |
108 view_name = callback.__class__.__name__ + '.__call__' # If it's a class |
144 # Reset URLconf for this thread on the way out for complete |
109 raise ValueError, "The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name) |
145 # isolation of request.urlconf |
110 |
146 urlresolvers.set_urlconf(None) |
111 return response |
|
112 except http.Http404, e: |
|
113 if settings.DEBUG: |
|
114 from django.views import debug |
|
115 return debug.technical_404_response(request, e) |
|
116 else: |
|
117 try: |
|
118 callback, param_dict = resolver.resolve404() |
|
119 return callback(request, **param_dict) |
|
120 except: |
|
121 try: |
|
122 return self.handle_uncaught_exception(request, resolver, sys.exc_info()) |
|
123 finally: |
|
124 receivers = signals.got_request_exception.send(sender=self.__class__, request=request) |
|
125 except exceptions.PermissionDenied: |
|
126 return http.HttpResponseForbidden('<h1>Permission denied</h1>') |
|
127 except SystemExit: |
|
128 # Allow sys.exit() to actually exit. See tickets #1023 and #4701 |
|
129 raise |
|
130 except: # Handle everything else, including SuspiciousOperation, etc. |
|
131 # Get the exception info now, in case another exception is thrown later. |
|
132 exc_info = sys.exc_info() |
|
133 receivers = signals.got_request_exception.send(sender=self.__class__, request=request) |
|
134 return self.handle_uncaught_exception(request, resolver, exc_info) |
|
135 |
147 |
136 def handle_uncaught_exception(self, request, resolver, exc_info): |
148 def handle_uncaught_exception(self, request, resolver, exc_info): |
137 """ |
149 """ |
138 Processing for any otherwise uncaught exceptions (those that will |
150 Processing for any otherwise uncaught exceptions (those that will |
139 generate HTTP 500 responses). Can be overridden by subclasses who want |
151 generate HTTP 500 responses). Can be overridden by subclasses who want |