Skip to content

Add interactive docs error template #5548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ class DocumentationRenderer(BaseRenderer):
format = 'html'
charset = 'utf-8'
template = 'rest_framework/docs/index.html'
error_template = 'rest_framework/docs/error.html'
code_style = 'emacs'
languages = ['shell', 'javascript', 'python']

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

def render(self, data, accepted_media_type=None, renderer_context=None):
template = loader.get_template(self.template)
context = self.get_context(data, renderer_context['request'])
return template.render(context, request=renderer_context['request'])
if isinstance(data, coreapi.Document):
template = loader.get_template(self.template)
context = self.get_context(data, renderer_context['request'])
return template.render(context, request=renderer_context['request'])
else:
template = loader.get_template(self.error_template)
context = {
"data": data,
"request": renderer_context['request'],
"response": renderer_context['response'],
"debug": settings.DEBUG,
}
return template.render(context, request=renderer_context['request'])


class SchemaJSRenderer(BaseRenderer):
Expand Down
75 changes: 75 additions & 0 deletions rest_framework/templates/rest_framework/docs/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Error Rendering Schema</title>
</head>
<body>



<h1>Error</h1>

<pre>
{{ data }}
</pre>


{% if debug is True %}
<hr />
<h2>Additional Information</h2>
<p>Note: You are seeing this message because <code>DEBUG==True</code>.</p>

<p>Seeing this page is <em>usually</em> a configuration error: are your
<code>DEFAULT_AUTHENTICATION_CLASSES</code> or <code>DEFAULT_PERMISSION_CLASSES</code>
being applied unexpectedly?</p>

<p>Your response status code is: <code>{{ response.status_code }}</code></p>

<h3>401 Unauthorised.</h3>
<ul>
<li>Do you have SessionAuthentication enabled?</li>
<li>Are you logged in?</li>
</ul>


<h3>403 Forbidden.</h3>
<ul>
<li>Do you have sufficient permissions to access this view?</li>
<li>Is you schema non-empty? (An empty schema will lead to a permission denied error being raised.)</li>
</ul>


<p>Most commonly the intended solution is to disable authentication and permissions
when including the docs urls:</p>

<pre>
url(r'^docs/', include_docs_urls(title='Your API',
authentication_classes=[],
permission_classes=[])),
</pre>


<h2>Overriding this template</h2>

<p>If you wish access to your docs to be authenticated you may override this template
at <code>rest_framework/docs/error.html</code>.</p>

<p>The available context is: <code>data</code> the error dict above, <code>request</code>,
<code>response</code> and the <code>debug</code> flag.</p>

{% endif %}



<script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script>
</body>
</html>