404使用tastypie访问API时出错

时间:2016-01-12 11:40:46

标签: django tastypie

刚开始使用TastyPie来公开数据。尝试使用tastypie.Api为urls.py绑定资源。

但是当我尝试通过localhost:api/**resource.

访问它时,我收到此错误

我的api.py:

from tastypie.resources import ModelResource
from idg.models.molecule_dictionary import MoleculeDictionary


class MoleculeDictionaryResource(ModelResource):
    class Meta:
        queryset = MoleculeDictionary.objects.all()
        # resource_name = 'moleculedictionary'

my url.py:

from django.conf.urls import url, include, patterns
from idg.api import MoleculeDictionaryResource
from django.contrib import admin
from tastypie.api import Api
from . import views

dictionary_resource = MoleculeDictionaryResource()
# private_api = Api(api_name='private')
# private_api.register(MoleculeDictionaryResource(), canonical=True)

urlpatterns = [
    url(r'^$', views.index, name='index'),
    # url(r'^exports/', include('data_exports.urls', namespace='data_exports')),
    url(r'^api/', include(dictionary_resource.urls)),
]

错误:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/api/moleculedictionary/?format=json

Using the URLconf defined in django_root.urls, Django tried these URL patterns, in this order:

    ^__debug__/
    xadmin/
    ^idg/
    ^comments/
    ^admin/

The current URL, api/moleculedictionary/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

有什么建议吗?我不确定我犯了什么错误。我已经按照教程(https://django-tastypie.readthedocs.org/en/latest/tutorial.html

进行了操作

2 个答案:

答案 0 :(得分:0)

取消注释行

resource_name = 'moleculedictionary'

在您的api.py文件中。

答案 1 :(得分:0)

试试这个:

private_api = Api(api_name='v1')
private_api.register(MoleculeDictionaryResource(), canonical=True)


url(r'^api/', include(private_api.urls)),

然后您应该可以使用/api/v1/

访问它