Django Rest 'api-docs' 不是注册的命名空间

标签 django django-rest-framework

我正在为我的 api 设置文档,当我要求 ( http://localhost:8000/api/v0/docs/ ) 时,该应用程序向我显示此错误:

NoReverseMatch at /api/v0/docs/
'api-docs' is not a registered namespace

当尝试这样做时
<script src="{% url **'api-docs:schema-js'** %}"></script>

这是我的 urls.py 文件:
from django.contrib import admin
from django.conf.urls import url, include
from django.contrib.auth.models import User
from . import views

from rest_framework import routers, serializers, viewsets

from rest_framework.documentation import include_docs_urls

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'goals', views.GoalViewSet)


urlpatterns = [
    url(r'^goals/$', include(router.urls)),
    url(r'^docs/', include_docs_urls(title='My API title')),
]

我将命名空间添加到 include_docs_urls 但它不起作用。 (我已经安装了 coreapi、pygments 和 markdown)。

在此先感谢您帮助我。

最佳答案

你应该移动你的 docs url 模式到您的 urls.py文件。这个还是DRF的问题,官方GitHub源上也有一个issue打开:https://github.com/encode/django-rest-framework/issues/4984

Django REST Framework 官方开发者,Tom Christie ,说:

It's important enough that I'd like to see it highly prioritized, but there's a lot of other things going on at the moment too. Even after we release 3.6.3, there's still also all the work towards 3.7.0's realtime integration.



所以你的 docs模式不应有任何前缀(如 api/v0/ )。

总之,改变你的主urls.py :
url(r'^docs/', include_docs_urls(title='API Title'))

关于Django Rest 'api-docs' 不是注册的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45829200/

相关文章:

python - forms.py 的问题

python - Django - 如何在同一模型的保存方法中过滤数据

python - 使 LiveServerTestCase 在每次测试之前不调用 setUp()

python - djangorest框架中查询参数的urlconf

python - 从 Django Rest Framework 中的 token 获取经过身份验证的用户

python - Django REST 框架 : "This field is required." with required=False and unique_together

python - 每个特定方法的 Django OAuth 工具包范围

django - 如果 'timesince' 超过 24 小时,则只显示天数而不是小时数

django - Django 中的 403 页

javascript - 即使在 React 中正确导入后也不包含默认导出