我为我的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)。
事先感谢帮助我。
答案 0 :(得分:4)
您应该将docs
网址格式移到主 urls.py
文件中。这仍然是DRF中的一个问题,GitHub官方消息来源也出现了问题:https://github.com/encode/django-rest-framework/issues/4984
Django REST Framework的官方开发人员Tom Christie说:
重要的是,我希望看到它具有高度优先级,但目前还有很多其他事情正在发生。即使在我们发布3.6.3之后,仍然存在着针对3.7.0实时集成的所有工作。
因此,您的docs
模式不应包含任何前缀(例如api/v0/
)。
总之,更改主urls.py
:
url(r'^docs/', include_docs_urls(title='API Title'))