|
29
|
1 |
from django.http import HttpResponseForbidden |
|
|
2 |
from django.template import Context, Template |
|
|
3 |
from django.conf import settings |
|
|
4 |
|
|
|
5 |
# We include the template inline since we need to be able to reliably display |
|
|
6 |
# this error message, especially for the sake of developers, and there isn't any |
|
|
7 |
# other way of making it available independent of what is in the settings file. |
|
|
8 |
|
|
|
9 |
CSRF_FAILRE_TEMPLATE = """ |
|
|
10 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
|
|
11 |
<html lang="en"> |
|
|
12 |
<head> |
|
|
13 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
14 |
<title>403 Forbidden</title> |
|
|
15 |
</head> |
|
|
16 |
<body> |
|
|
17 |
<h1>403 Forbidden</h1> |
|
|
18 |
<p>CSRF verification failed. Request aborted.</p> |
|
|
19 |
{% if DEBUG %} |
|
|
20 |
<h2>Help</h2> |
|
|
21 |
{% if reason %} |
|
|
22 |
<p>Reason given for failure:</p> |
|
|
23 |
<pre> |
|
|
24 |
{{ reason }} |
|
|
25 |
</pre> |
|
|
26 |
{% endif %} |
|
|
27 |
|
|
|
28 |
<p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when |
|
|
29 |
<a |
|
|
30 |
href='http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ref-contrib-csrf'>Django's |
|
|
31 |
CSRF mechanism</a> has not been used correctly. For POST forms, you need to |
|
|
32 |
ensure:</p> |
|
|
33 |
|
|
|
34 |
<ul> |
|
|
35 |
<li>The view function uses <a |
|
|
36 |
href='http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext'><code>RequestContext</code></a> |
|
|
37 |
for the template, instead of <code>Context</code>.</li> |
|
|
38 |
|
|
|
39 |
<li>In the template, there is a <code>{% templatetag openblock %} csrf_token |
|
|
40 |
{% templatetag closeblock %}</code> template tag inside each POST form that |
|
|
41 |
targets an internal URL.</li> |
|
|
42 |
|
|
|
43 |
<li>If you are not using <code>CsrfViewMiddleware</code>, then you must use |
|
|
44 |
<code>csrf_protect</code> on any views that use the <code>csrf_token</code> |
|
|
45 |
template tag, as well as those that accept the POST data.</li> |
|
|
46 |
|
|
|
47 |
</ul> |
|
|
48 |
|
|
|
49 |
<p>You're seeing the help section of this page because you have <code>DEBUG = |
|
|
50 |
True</code> in your Django settings file. Change that to <code>False</code>, |
|
|
51 |
and only the initial error message will be displayed. </p> |
|
|
52 |
|
|
|
53 |
<p>You can customize this page using the CSRF_FAILURE_VIEW setting.</p> |
|
|
54 |
{% else %} |
|
|
55 |
<p><small>More information is available with DEBUG=True.</small></p> |
|
|
56 |
|
|
|
57 |
{% endif %} |
|
|
58 |
</body> |
|
|
59 |
</html> |
|
|
60 |
""" |
|
|
61 |
|
|
|
62 |
def csrf_failure(request, reason=""): |
|
|
63 |
""" |
|
|
64 |
Default view used when request fails CSRF protection |
|
|
65 |
""" |
|
|
66 |
t = Template(CSRF_FAILRE_TEMPLATE) |
|
|
67 |
c = Context({'DEBUG': settings.DEBUG, |
|
|
68 |
'reason': reason}) |
|
|
69 |
return HttpResponseForbidden(t.render(c), mimetype='text/html') |