使用tastypie编写APIS。 urls.py中出错

时间:2012-08-20 11:21:48

标签: python django tastypie

name 'entry_resource' is not defined

这是我的models.py

from tastypie.utils.timezone import now
from django.contrib.auth.models import User
from django.db import models
from django.template.defaultfilters import slugify


class Entry(models.Model):
    user = models.ForeignKey(User)
    pub_date = models.DateTimeField(default=now)
    title = models.CharField(max_length=200)
    slug = models.SlugField()
    body = models.TextField()

    def __unicode__(self):
        return self.title

    def save(self, *args, **kwargs):
        # For automatic slug generation.
        if not self.slug:
            self.slug = slugify(self.title)[:50]

        return super(Entry, self).save(*args, **kwargs)

这是我的api.py:

from tastypie.resources import ModelResource
from links.models import Entry


class EntryResource(ModelResource):
    class Meta:
        queryset = Entry.objects.all()
        resource_name = 'entry'

这是我的urls.py

from django.conf.urls import patterns, include, url
from links.api import EntryResource
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
     (r'^api/', include(entry_resource.urls)),
)

我很确定我做的事情很傻。虽然不能找到什么。

1 个答案:

答案 0 :(得分:4)

来自官方tastypie文档(https://github.com/toastdriven/django-tastypie#whats-it-look-like):

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

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

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