Repository: encode/django-rest-framework

Issue:
Document not being generated correctly in docs
## Checklist

- [ ] I have verified that that issue exists against the `master` branch of Django REST framework.
- [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [x] This is not a usage question. (Those should be directed to the [discussion group](https://groups.google.com/forum/#!forum/django-rest-framework) instead.)
- [x] This cannot be dealt with as a third party library. (We prefer new functionality to be [in the form of third party libraries](http://www.django-rest-framework.org/topics/third-party-resources/#about-third-party-packages) where possible.)
- [ ] I have reduced the issue to the simplest possible case.
- [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

## Steps to reproduce

## Expected behavior
Should display docs, when visiting /docs/

## Actual behavior
Error in template documentation/sidebar.html,  'dict' object has no attribute 'data'

It looks like the document is not being generated with data, also document.links is missing.


![screenshot from 2017-10-13 11-21-30](https://user-images.githubusercontent.com/8204177/31539597-5a690ab2-b009-11e7-84fe-d64c09c9041a.png)




Discussion:
There's little we can do without more context.
Do you have a simple test case we can reproduce ?
Will reopen if more informations are provided.
Steps to reproduce, in my case

Following steps in documentation:

http://www.django-rest-framework.org/topics/documenting-your-api/
Just went through those steps again and worked on my pet project.
@upatricck This looks similar to #5472 and #5395. (Are you running 3.7.0? Do you have `link` named `data`?)

This works under usual circumstances so you need to do some work to tell us why it's failing for you. Other we cannot help. 

One thought is that we're expecting to see a `coreapi.Document` here. Your error is suggesting you've got a `dict`. Where's that `dict` coming from? 
I got the same problem:
```
File "/home/kuboon/kikof/env/local/lib/python2.7/site-packages/rest_framework/templatetags/rest_framework.py", line 261, in data
    return value.data
AttributeError: 'dict' object has no attribute 'data'
```
And value was:
`{u'detail': u'Authentication credentials were not provided.'}` 

My temp fix:
```
from rest_framework.permissions import AllowAny
url(r'^api/new-docs/', include_docs_urls(..., permission_classes=(AllowAny,))),
```
I don't use sessions, only token auth

@kub00n Some thoughts: 

* What are your authentication classes here? (Your defaults if you're not setting any?) If you disable authentication — `authentication_classes=[]` — does it work? 
* I'm not sure authentication without Session Auth makes sense here. You're viewing a web page — either you're logged in (with the Session) or you're not. 
* What's the behaviour we want for an authentication failure here? (403, rather than 500)
* (Why I|i)s the renderer setting the error dict as the `document` value in the template context? 

I'm going to reopen this to give time to investigate.
I encountered this when I created a new project for DRF, and I hadn't yet created any URLs for DRF, I only had classic Django views.   After I added my first URLs by using a DRF SimpleRouter, the problem went away.
This looks like we're just not handling the empty case correctly. (Some auth issue, no schema, etc.) Should be easy enough to clean up. 
Right, I'm able to reproduce this a few ways: 

1. You have any kind of `IsAuthenticated` permission as your default and or explicitly on your `SchemaView`, or call to `include_docs_url` and are either logged out, or do not have `SessionAuthentication` enabled.
2. You are logged in but do not have permissions to access the view. 
3. Your call to SchemaGenerator returns `None` (i.e. no routed APIViews). (This last case raises `PermissionDenied`, so it reduces to 2.)



I was able to reproduce this bug by explicitly passing auth token in headers (using chrome extension), while using default drf ```TokenAuthentication```.
Default permission is ```AllowAny```
@Zeliboba5: Just to be certain, what's the value of `document` in the context when you do this? (i.e. what's the error dict?) Thanks. 
@carltongibson There is no ```document``` in context, but it only value in context says about user being inactive, which is true. If I activate user - problem does not occur, sorry if previous comment was misleading about that. 
Fine. That’s case 1 again. No problem. Thank you!

Resolving patch:
diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py
--- a/rest_framework/renderers.py
+++ b/rest_framework/renderers.py
@@ -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']
 
@@ -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):


Test patch: