Skip to content

Commit 565c722

Browse files
Add interactive docs error template (#5548)
1 parent e5cee43 commit 565c722

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

rest_framework/renderers.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ class DocumentationRenderer(BaseRenderer):
812812
format = 'html'
813813
charset = 'utf-8'
814814
template = 'rest_framework/docs/index.html'
815+
error_template = 'rest_framework/docs/error.html'
815816
code_style = 'emacs'
816817
languages = ['shell', 'javascript', 'python']
817818

@@ -824,9 +825,19 @@ def get_context(self, data, request):
824825
}
825826

826827
def render(self, data, accepted_media_type=None, renderer_context=None):
827-
template = loader.get_template(self.template)
828-
context = self.get_context(data, renderer_context['request'])
829-
return template.render(context, request=renderer_context['request'])
828+
if isinstance(data, coreapi.Document):
829+
template = loader.get_template(self.template)
830+
context = self.get_context(data, renderer_context['request'])
831+
return template.render(context, request=renderer_context['request'])
832+
else:
833+
template = loader.get_template(self.error_template)
834+
context = {
835+
"data": data,
836+
"request": renderer_context['request'],
837+
"response": renderer_context['response'],
838+
"debug": settings.DEBUG,
839+
}
840+
return template.render(context, request=renderer_context['request'])
830841

831842

832843
class SchemaJSRenderer(BaseRenderer):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{% load staticfiles %}
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
9+
<title>Error Rendering Schema</title>
10+
</head>
11+
<body>
12+
13+
14+
15+
<h1>Error</h1>
16+
17+
<pre>
18+
{{ data }}
19+
</pre>
20+
21+
22+
{% if debug is True %}
23+
<hr />
24+
<h2>Additional Information</h2>
25+
<p>Note: You are seeing this message because <code>DEBUG==True</code>.</p>
26+
27+
<p>Seeing this page is <em>usually</em> a configuration error: are your
28+
<code>DEFAULT_AUTHENTICATION_CLASSES</code> or <code>DEFAULT_PERMISSION_CLASSES</code>
29+
being applied unexpectedly?</p>
30+
31+
<p>Your response status code is: <code>{{ response.status_code }}</code></p>
32+
33+
<h3>401 Unauthorised.</h3>
34+
<ul>
35+
<li>Do you have SessionAuthentication enabled?</li>
36+
<li>Are you logged in?</li>
37+
</ul>
38+
39+
40+
<h3>403 Forbidden.</h3>
41+
<ul>
42+
<li>Do you have sufficient permissions to access this view?</li>
43+
<li>Is you schema non-empty? (An empty schema will lead to a permission denied error being raised.)</li>
44+
</ul>
45+
46+
47+
<p>Most commonly the intended solution is to disable authentication and permissions
48+
when including the docs urls:</p>
49+
50+
<pre>
51+
url(r'^docs/', include_docs_urls(title='Your API',
52+
authentication_classes=[],
53+
permission_classes=[])),
54+
</pre>
55+
56+
57+
<h2>Overriding this template</h2>
58+
59+
<p>If you wish access to your docs to be authenticated you may override this template
60+
at <code>rest_framework/docs/error.html</code>.</p>
61+
62+
<p>The available context is: <code>data</code> the error dict above, <code>request</code>,
63+
<code>response</code> and the <code>debug</code> flag.</p>
64+
65+
{% endif %}
66+
67+
68+
69+
<script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script>
70+
</body>
71+
</html>
72+
73+
74+
75+

0 commit comments

Comments
 (0)