django tastypie网址无效

时间:2013-03-29 18:00:05

标签: django tastypie

我似乎无法弄清楚如何使tastypie URL工作。 当我在浏览器中转到我的应用程序的根目录时,调试不会列出该URL。 我错过了什么?

## inventory/models.py
from django.db import models
#
class Server (models.Model):
    server_id  = models.AutoField(primary_key=True)
    server_name = models.CharField("Server Name",max_length=200,unique=True)
    server_ip = models.IPAddressField("Server IP")
    server_serial = models.CharField("Serial No.",max_length=25,null=True,blank=True)
    server_mem = models.PositiveIntegerField("Mem (MB)",null=True,blank=True)
    server_architecture = models.ForeignKey(Architecture)
    server_os = models.ForeignKey(Operating_System)
    server_os_version = models.CharField("OS   Version",max_length="25",null=True,blank=True)
    server_type = models.ForeignKey(Server_Type,null=True,blank=True)
    server_mac = models.ForeignKey(Mac,null=True,blank=True)
    server_digi = models.ForeignKey(Digi,null=True,blank=True)
    server_digi_port = models.PositiveIntegerField("Digi Port",null=True,blank=True)
    server_rack = models.ForeignKey(Rack,null=True,blank=True)
    server_kvm = models.ForeignKey(KVM,null=True,blank=True)
    def __unicode__(self):
            return self.server_name

## inventory/api.py
from tastypie.resources import ModelResource
from inventory.models import *
#
class ServerResource(ModelResource):
 class Meta:
    queryset = Server.objects.all()
    resource_name = 'servers'

## mmi_assets/urls.py
from django.conf.urls import patterns, include, url
#
## our custom stuff
from inventory.api import *
server_resource = ServerResource()
##

urlpatterns = patterns('',
  url(r'^admin/', include(admin.site.urls)),
  url(r'^inventory/', include('inventory.urls')),
  url(r'^api/', include(server_resource.urls)),
)

1 个答案:

答案 0 :(得分:1)

您需要将资源添加到api,然后包含api的url。 http://django-tastypie.readthedocs.org/en/latest/tutorial.html#adding-to-the-api

# urls.py
from django.conf.urls.defaults import *
from tastypie.api import Api
from myapp.api import EntryResource, UserResource

v1_api = Api(api_name='v1')
v1_api.register(UserResource())
v1_api.register(EntryResource())

urlpatterns = patterns('',
    # The normal jazz here...
    (r'^blog/', include('myapp.urls')),
    (r'^api/', include(v1_api.urls)),
)