Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14b0132

Browse files
yushanfans2233felixxm
authored andcommitted
Fixed #34830 -- Added request to bad_request/csrf_failure view template contexts.
1 parent 8fcb9f1 commit 14b0132

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

django/views/csrf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ def csrf_failure(request, reason="", template_name=CSRF_FAILURE_TEMPLATE_NAME):
6767
}
6868
try:
6969
t = loader.get_template(template_name)
70+
body = t.render(request=request)
7071
except TemplateDoesNotExist:
7172
if template_name == CSRF_FAILURE_TEMPLATE_NAME:
7273
# If the default template doesn't exist, use the fallback template.
7374
with builtin_template_path("csrf_403.html").open(encoding="utf-8") as fh:
7475
t = Engine().from_string(fh.read())
75-
c = Context(c)
76+
body = t.render(Context(c))
7677
else:
7778
# Raise if a developer-specified template doesn't exist.
7879
raise
79-
return HttpResponseForbidden(t.render(c))
80+
return HttpResponseForbidden(body)

django/views/defaults.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def bad_request(request, exception, template_name=ERROR_400_TEMPLATE_NAME):
109109
"""
110110
try:
111111
template = loader.get_template(template_name)
112+
body = template.render(request=request)
112113
except TemplateDoesNotExist:
113114
if template_name != ERROR_400_TEMPLATE_NAME:
114115
# Reraise if it's a missing custom template.
@@ -118,7 +119,7 @@ def bad_request(request, exception, template_name=ERROR_400_TEMPLATE_NAME):
118119
)
119120
# No exception content is passed to the template, to not disclose any
120121
# sensitive information.
121-
return HttpResponseBadRequest(template.render())
122+
return HttpResponseBadRequest(body)
122123

123124

124125
@requires_csrf_token

tests/view_tests/tests/test_csrf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def test_custom_template(self):
112112
"""A custom CSRF_FAILURE_TEMPLATE_NAME is used."""
113113
response = self.client.post("/")
114114
self.assertContains(response, "Test template for CSRF failure", status_code=403)
115+
self.assertIs(response.wsgi_request, response.context.request)
115116

116117
def test_custom_template_does_not_exist(self):
117118
"""An exception is raised if a nonexistent template is supplied."""

tests/view_tests/tests/test_defaults.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ def test_bad_request(self):
102102
response = bad_request(request, Exception())
103103
self.assertContains(response, b"<h1>Bad Request (400)</h1>", status_code=400)
104104

105+
@override_settings(
106+
TEMPLATES=[
107+
{
108+
"BACKEND": "django.template.backends.django.DjangoTemplates",
109+
"OPTIONS": {
110+
"loaders": [
111+
(
112+
"django.template.loaders.locmem.Loader",
113+
{
114+
"400.html": (
115+
"This is a test template for a 400 error "
116+
),
117+
},
118+
),
119+
],
120+
},
121+
}
122+
]
123+
)
124+
def test_custom_bad_request_template(self):
125+
response = self.client.get("/raises400/")
126+
self.assertIs(response.wsgi_request, response.context[-1].request)
127+
105128
@override_settings(
106129
TEMPLATES=[
107130
{

0 commit comments

Comments
 (0)