Ticket #17449: ticket-17449-default-options.patch
File ticket-17449-default-options.patch, 4.8 KB (added by , 13 years ago) |
---|
-
docs/ref/request-response.txt
748 748 The constructor doesn't take any arguments. Use this to designate that a 749 749 page hasn't been modified since the user's last request (status code 304). 750 750 751 .. class:: HttpResponseOptions 752 753 Like :class:`HttpResponse`, but uses a 200 status code and provides 754 an ``Allow`` response header. Takes a single, required argument: a list 755 of permitted methods (e.g., ``['GET', 'POST']``) used to populate 756 the ``Allow`` header. 757 751 758 .. class:: HttpResponseBadRequest 752 759 753 760 Acts just like :class:`HttpResponse` but uses a 400 status code. -
tests/regressiontests/generic_views/base.py
170 170 """ 171 171 self.assertTrue(DecoratedDispatchView.as_view().is_decorated) 172 172 173 def test_head_no_get(self): 174 """ 175 Test that a view class with no get responds to a HEAD request with HTTP 176 405. 177 """ 178 request = self.rf.head('/') 179 view = PostOnlyView.as_view() 180 self.assertEqual(405, view(request).status_code) 173 181 182 def test_options(self): 183 """ 184 Test that views respond to HTTP OPTIONS requests with an Allow header 185 appropriate for the methods implemented by the view class. 186 """ 187 request = self.rf.options('/') 188 view = SimpleView.as_view() 189 response = view(request) 190 self.assertEqual(200, response.status_code) 191 self.assertTrue(response['Allow']) 192 193 def test_options_for_get_view(self): 194 """ 195 Test that a view implementing GET allows GET and HEAD. 196 """ 197 request = self.rf.options('/') 198 view = SimpleView.as_view() 199 response = view(request) 200 self._assert_allows(response, 'GET', 'HEAD') 201 202 def test_options_for_get_and_post_view(self): 203 """ 204 Test that a view implementing GET and POST allows GET, HEAD, and POST. 205 """ 206 request = self.rf.options('/') 207 view = SimplePostView.as_view() 208 response = view(request) 209 self._assert_allows(response, 'GET', 'HEAD', 'POST') 210 211 def test_options_for_post_view(self): 212 """ 213 Test that a view implementing POST allows POST. 214 """ 215 request = self.rf.options('/') 216 view = PostOnlyView.as_view() 217 response = view(request) 218 self._assert_allows(response, 'POST') 219 220 def _assert_allows(self, response, *expected_methods): 221 "Assert allowed HTTP methods reported in the Allow response header" 222 response_allows = set(response['Allow'].split(', ')) 223 self.assertEqual(set(expected_methods + ('OPTIONS',)), response_allows) 224 225 174 226 class TemplateViewTest(TestCase): 175 227 urls = 'regressiontests.generic_views.urls' 176 228 -
django/http/__init__.py
726 726 raise Exception("This %s instance cannot tell its position" % self.__class__) 727 727 return sum([len(str(chunk)) for chunk in self._container]) 728 728 729 class HttpResponseOptions(HttpResponse): 730 status_code = 200 731 732 def __init__(self, permitted_methods): 733 super(HttpResponseOptions, self).__init__() 734 self['Allow'] = ', '.join(permitted_methods) 735 self['Content-Length'] = '0' 736 729 737 class HttpResponseRedirect(HttpResponse): 730 738 status_code = 302 731 739 -
django/views/generic/base.py
69 69 return handler(request, *args, **kwargs) 70 70 71 71 def http_method_not_allowed(self, request, *args, **kwargs): 72 allowed_methods = [m for m in self.http_method_names if hasattr(self, m)]73 72 logger.warning('Method Not Allowed (%s): %s', request.method, request.path, 74 73 extra={ 75 74 'status_code': 405, 76 75 'request': self.request 77 76 } 78 77 ) 79 return http.HttpResponseNotAllowed( allowed_methods)78 return http.HttpResponseNotAllowed(self._allowed_methods()) 80 79 80 def options(self, request, *args, **kwargs): 81 return http.HttpResponseOptions(self._allowed_methods()) 81 82 83 def _allowed_methods(self): 84 return [m.upper() for m in self.http_method_names if hasattr(self, m)] 85 86 82 87 class TemplateResponseMixin(object): 83 88 """ 84 89 A mixin that can be used to render a template.