我正在为我的 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/