Ticket #9847: 9847.diff
File 9847.diff, 7.6 KB (added by , 14 years ago) |
---|
-
django/conf/urls/defaults.py
1 1 from django.core.urlresolvers import RegexURLPattern, RegexURLResolver 2 2 from django.core.exceptions import ImproperlyConfigured 3 3 4 __all__ = ['handler40 4', 'handler500', 'include', 'patterns', 'url']4 __all__ = ['handler403', 'handler404', 'handler500', 'include', 'patterns', 'url'] 5 5 6 handler403 = 'django.views.defaults.permission_denied' 6 7 handler404 = 'django.views.defaults.page_not_found' 7 8 handler500 = 'django.views.defaults.server_error' 8 9 -
django/core/urlresolvers.py
295 295 except (ImportError, AttributeError), e: 296 296 raise ViewDoesNotExist("Tried %s. Error was: %s" % (callback, str(e))) 297 297 298 def resolve403(self): 299 return self._resolve_special('403') 300 298 301 def resolve404(self): 299 302 return self._resolve_special('404') 300 303 -
django/core/handlers/base.py
131 131 finally: 132 132 receivers = signals.got_request_exception.send(sender=self.__class__, request=request) 133 133 except exceptions.PermissionDenied: 134 return http.HttpResponseForbidden('<h1>Permission denied</h1>') 134 try: 135 callback, param_dict = resolver.resolve403() 136 return callback(request, **param_dict) 137 except: 138 return http.HttpResponseForbidden('<h1>403 Forbidden</h1>') 135 139 except SystemExit: 136 140 # Allow sys.exit() to actually exit. See tickets #1023 and #4701 137 141 raise -
django/views/defaults.py
1 1 from django import http 2 2 from django.template import Context, RequestContext, loader 3 3 4 def permission_denied(request, template_name='403.html'): 5 t = loader.get_template(template_name) # You need to create a 403.html template. 6 return http.HttpResponseForbidden(t.render(Context({'request_path': request.path}))) 7 4 8 def page_not_found(request, template_name='404.html'): 5 9 """ 6 10 Default 404 handler. -
tests/regressiontests/views/tests/debug.py
1 1 import inspect 2 from os import path, remove 2 3 3 4 from django.conf import settings 4 5 from django.core.files.uploadedfile import SimpleUploadedFile … … 30 31 self.failUnless('file_data.txt' in response.content) 31 32 self.failIf('haha' in response.content) 32 33 34 def test_403(self): 35 # Can't use NamedTemporaryFile since it has to be in ../templates 36 37 # Create a 403.html and check if it is served to us when a 403 is triggered 38 template_path = '../templates/403.html' 39 if not path.exists(template_path): 40 FILE = open(template_path) 41 FILE.write('This is a test template for a 403 Forbidden error.') 42 FILE.close() 43 self.failIf(not path.exists(template_path) 44 response_with_template = self.client.get('/views/raises403') 45 self.assertEquals(response_with_template.status_code, 403) 46 self.failUnless('template' in response_with_template.content) 47 remove(template_path) 48 self.failIf(path.exists(template_path)) 49 50 # See if the default thing happens when a 403 is triggered, and we *don't* have a 403.html 51 response_without_template = self.client.get('/views/raises403/') 52 self.assertEquals(response_without_template.status_code, 403) 53 self.failIf('template' in response_without_template.content) 54 33 55 def test_404(self): 34 56 response = self.client.get('/views/raises404/') 35 57 self.assertEquals(response.status_code, 404) -
tests/regressiontests/views/views.py
6 6 from django.views.generic.create_update import create_object 7 7 from django.core.urlresolvers import get_resolver 8 8 from django.shortcuts import render_to_response 9 from django.core.exceptions import PermissionDenied 9 10 10 11 from regressiontests.views import BrokenException, except_args 11 12 … … 44 45 resolver = get_resolver(None) 45 46 resolver.resolve('') 46 47 48 def raises403(request): 49 raise PermissionDenied() 50 47 51 def redirect(request): 48 52 """ 49 53 Forces an HTTP redirect. … … 56 60 def template_exception(request, n): 57 61 return render_to_response('debug/template_exception.html', 58 62 {'arg': except_args[int(n)]}) 59 -
tests/regressiontests/views/urls.py
109 109 urlpatterns += patterns('', 110 110 (r'^raises/$', views.raises), 111 111 (r'^raises404/$', views.raises404), 112 (r'^raises403/$', views.raises403), 112 113 ) 113 114 114 115 # rediriects, both temporary and permanent, with non-ASCII targets -
AUTHORS
430 430 Ben Slavin <benjamin.slavin@gmail.com> 431 431 sloonz <simon.lipp@insa-lyon.fr> 432 432 Paul Smith <blinkylights23@gmail.com> 433 Steven L. Smith (fvox13) <steven@stevenlsmith.com> 433 434 Warren Smith <warren@wandrsmith.net> 434 435 smurf@smurf.noris.de 435 436 Vsevolod Solovyov -
docs/topics/http/views.txt
196 196 That takes care of setting ``handler500`` in the current module. As you can see 197 197 in ``django/conf/urls/defaults.py``, ``handler500`` is set to 198 198 ``'django.views.defaults.server_error'`` by default. 199 200 The 403 (HTTP Forbidden) view 201 ---------------------------- 202 203 In the same vein as the 404 and 500 views, Django has a view to handle 403 Forbidden 204 errors. If a view results in a 403 exception, Django will, by default, call the view 205 ``django.views.defaults.permission_denied``, which loads and renders the template ``403.html``. 206 207 If you do not provide a ``403.html`` template in your root template directory, this 208 view will instead serve the text "403 Forbidden", as per RFC 2616 (the HTTP 1.1 Specification). 209 210 It is possible to override ``django.views.defaults.permission_denied`` in much the same way you 211 can for the 404 and 500 views. -
docs/ref/exceptions.txt
48 48 ---------------- 49 49 50 50 The ``PermissionDenied`` exception is raised when a user does not have 51 permission to perform the action requested. 51 permission to perform the action requested. It is equivalent to an HTTP 403 Error. 52 52 53 53 ViewDoesNotExist 54 54 ----------------