~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/views/defaults.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import warnings
 
2
 
1
3
from django import http
2
4
from django.template import (Context, RequestContext,
3
5
                             loader, Template, TemplateDoesNotExist)
19
21
    """
20
22
    try:
21
23
        template = loader.get_template(template_name)
 
24
        content_type = None             # Django will use DEFAULT_CONTENT_TYPE
22
25
    except TemplateDoesNotExist:
23
26
        template = Template(
24
27
            '<h1>Not Found</h1>'
25
28
            '<p>The requested URL {{ request_path }} was not found on this server.</p>')
26
 
    return http.HttpResponseNotFound(template.render(RequestContext(request, {'request_path': request.path})))
 
29
        content_type = 'text/html'
 
30
    body = template.render(RequestContext(request, {'request_path': request.path}))
 
31
    return http.HttpResponseNotFound(body, content_type=content_type)
27
32
 
28
33
 
29
34
@requires_csrf_token
37
42
    try:
38
43
        template = loader.get_template(template_name)
39
44
    except TemplateDoesNotExist:
40
 
        return http.HttpResponseServerError('<h1>Server Error (500)</h1>')
 
45
        return http.HttpResponseServerError('<h1>Server Error (500)</h1>', content_type='text/html')
41
46
    return http.HttpResponseServerError(template.render(Context({})))
42
47
 
43
48
 
 
49
@requires_csrf_token
 
50
def bad_request(request, template_name='400.html'):
 
51
    """
 
52
    400 error handler.
 
53
 
 
54
    Templates: :template:`400.html`
 
55
    Context: None
 
56
    """
 
57
    try:
 
58
        template = loader.get_template(template_name)
 
59
    except TemplateDoesNotExist:
 
60
        return http.HttpResponseBadRequest('<h1>Bad Request (400)</h1>', content_type='text/html')
 
61
    return http.HttpResponseBadRequest(template.render(Context({})))
 
62
 
 
63
 
44
64
# This can be called when CsrfViewMiddleware.process_view has not run,
45
65
# therefore need @requires_csrf_token in case the template needs
46
66
# {% csrf_token %}.
58
78
    try:
59
79
        template = loader.get_template(template_name)
60
80
    except TemplateDoesNotExist:
61
 
        return http.HttpResponseForbidden('<h1>403 Forbidden</h1>')
 
81
        return http.HttpResponseForbidden('<h1>403 Forbidden</h1>', content_type='text/html')
62
82
    return http.HttpResponseForbidden(template.render(RequestContext(request)))
63
83
 
64
84
 
65
85
def shortcut(request, content_type_id, object_id):
66
 
    # TODO: Remove this in Django 2.0.
67
 
    # This is a legacy view that depends on the contenttypes framework.
68
 
    # The core logic was moved to django.contrib.contenttypes.views after
69
 
    # Django 1.0, but this remains here for backwards compatibility.
70
 
    # Note that the import is *within* this function, rather than being at
71
 
    # module level, because we don't want to assume people have contenttypes
72
 
    # installed.
 
86
    warnings.warn(
 
87
        "django.views.defaults.shortcut will be removed in Django 1.8. "
 
88
        "Import it from django.contrib.contenttypes.views instead.",
 
89
        PendingDeprecationWarning, stacklevel=2)
73
90
    from django.contrib.contenttypes.views import shortcut as real_shortcut
74
91
    return real_shortcut(request, content_type_id, object_id)