繁体   English   中英

为什么Django-rest-swagger不能找到我的api路径?

[英]Why can't Django-rest-swagger find my api paths?

我已经安装了Django-Rest-Swagger并尽我所能来设置它。 从查看文档来看,我真的不明白我是否需要做更多工作才能使它工作。

如果我去localhost:800/docs我看到这个:

swagger just sits looking for resources

如果我导航到该URL,我会看到这个JSON:

The Json at localhost:8000/docs/api-docs with my three api paths

那么如果存在,那会阻止它找到它们吗?

我不认为我有一个模型序列化器,我不明白为什么会这样。 我也发现了这一点 ,但我认为不是这样。


Swagger设置:

SWAGGER_SETTINGS = {
    'exclude_namespaces': [],
    'api_version': '0.1',
    'api_path': '/characters/api',
    'enabled_methods': [
        'get',
        'post',
        'put',
        'patch',
        'delete'
    ],
    'api_key': '',
    'is_authenticated': False,
    'is_superuser': False,
    'permission_denied_handler': None,
    'info': {
        'contact': 'froo@barr.com',
        'description': 'This is a nWoD-DB Api thing',
        'license': 'No Licence',
        'licenseUrl': '',
        'termsOfServiceUrl': '',
        'title': 'nWoD-DB',
    },
    'doc_expansion': 'none',
}

Urls.py

#urls.py
urlpatterns = patterns('',
                       # Examples:
                       # url(r'^$', 'nwod_characters.views.home', name='home'),
                       # url(r'^blog/', include('blog.urls')),

                       url(r'^admin/', include(admin.site.urls)),
                       url(r'^characters/', include('characters.urls')),
                       url(r'^djangular/', include('djangular.urls')),
                       url(r'^api-auth/', include('rest_framework.urls',
                                                  namespace='rest_framework')),
                       url(r'^docs/',
                           include('rest_framework_swagger.urls')),
                       )
#characters/urls.py
mage_list = MageViewSet.as_view({
    'get': 'list',
    'post': 'create'
})

mage_detail = MageViewSet.as_view({
    'get': 'retrieve',
    'put': 'update',
    'patch': 'partial_update',
    'delete': 'destroy'
})
user_list = UserViewSet.as_view({
    'get': 'list'
})
user_detail = UserViewSet.as_view({
    'get': 'retrieve'
})


urlpatterns = format_suffix_patterns([
    url(r'^api/root$', api_root),
    url(r'^api/mages$', mage_list, name='mage-list'),
    url(r'^api/mages/(?P<pk>[0-9]+)$', mage_detail, name='mage-detail'),
    url(r'^api/users$', user_list, name='user-list'),
    url(r'^api/users/(?P<pk>[0-9]+)$', user_detail, name='user-detail'),
])
urlpatterns += [url(r'^$', IndexView.as_view(), name='index'), ]

“问题”是你正在做“include('characters.urls')”就我所知,rest_framework_swagger很难找到你的根urlpatterns中没有明确显示的任何网址 - 如果你想保证swagger会发现他们你必须将它们扩展到你的urlpatterns而不是从另一个文件中包含它们,即:

urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'nwod_characters.views.home', name='home'),
        # url(r'^blog/', include('blog.urls')),

        url(r'^admin/', include(admin.site.urls)),
        url(r'^api/root$', api_root),
        url(r'^api/mages$', mage_list, name='mage-list'),
        url(r'^api/mages/(?P<pk>[0-9]+)$', mage_detail, name='mage-detail'),
        url(r'^api/users$', user_list, name='user-list'),
        url(r'^api/users/(?P<pk>[0-9]+)$', user_detail, name='user-detail'),
....

我很高兴知道是否有人有办法配置招摇,以避免不得不把所有的网址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM