0

我似乎无法弄清楚如何使美味的 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)),
)
4

1 回答 1

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)),
)
于 2013-03-29T19:01:11.623 回答