【问题标题】:Django REST Framework error with accepted_renderer带有接受的渲染器的 Django REST 框架错误
【发布时间】:2019-01-06 05:05:30
【问题描述】:

我对 DFR 非常陌生,因此我正在按照教程进行学习。 到目前为止一切都很好,但在最后一部分,here,我必须向 API 添加一个模式并按照说明进行操作,所以安装 coreapi 并添加

from rest_framework.schemas import get_schema_view

schema_view = get_schema_view(title='Pastebin API')

urlpatterns = [
    path('schema/', schema_view),
    ...
]

在tutorial/urls.py中,所以我完整的tutorial/urls.py是:

from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include

from rest_framework.schemas import get_schema_view

schema_view = get_schema_view(title='Pastebin API')

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    path('', include('snippets.urls')),
    path('api-auth/', include('rest_framework.urls')),
    path('schema/', schema_view),
]

但如果我从浏览器http://127.0.0.1:8000/schema/ 访问/schema/,我会得到:

AttributeError at /schema/

'Request' object has no attribute 'accepted_renderer'

我不明白如何解决此错误,因为我按照教程的说明进行操作。

我的配置是:

coreapi             2.3.3     
coreschema          0.0.4     
Django              2.1.3     
djangorestframework 3.9.0  

如果需要,您可以查看教程的代码 herehere 是 get_schema_view 文档。

【问题讨论】:

标签: rest api django-rest-framework


【解决方案1】:

有两种解决方案:

安装pyyaml:

pip install pyyaml

还原 DRF:

pip install djangorestframework==3.8.0

【讨论】:

    【解决方案2】:

    本教程使用HTTPie 从命令行向该端点发出请求,而不是需要指定渲染器的浏览器。如果您希望它在浏览器中工作,请指定一个渲染器,如 documentation 描述的那样

    【讨论】:

    • 教程说:如果您在浏览器中访问 /schema/ 端点,您现在应该会看到 corejson 表示作为一个选项可用。
    • 不,它没有,我查看了教程中get_schema_view 的文档和您的链接,它声明Once the view has been added, you'll be able to make API requests to retrieve the auto-generated schema definition.,然后是使用HTTPie 的请求。
    • 是的,我知道并且我已经尝试过使用 httpie 并得到同样的错误。此外,我已经从浏览器和命令行测试了所有其他端点(来自本教程的前一部分),只有这不起作用。请注意,请求可以来自浏览器和命令行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多