为什么Django-rest-swagger无法找到我的api路径?

时间:2015-03-05 10:28:29

标签: python django rest django-rest-framework swagger

我已经安装了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

所以,如果存在这些,会阻止它找到它们吗?

我不认为我有model serializer我不明白为什么会这样。我也找到了this,但我认为不是这样。


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'), ]

1 个答案:

答案 0 :(得分:0)

'问题'是你正在做“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'),
....

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