一.首先安装tastypie
pip install django-tastypie
pip install defusedxml "lxml>=3"
二.添加article/api.py,内容如下
from tastypie.resources import ModelResource
from tastypie.constants import ALL
from models import Article
class ArticleResource(ModelResource):
class Meta:
queryset = Article.objects.all()
resource_name = 'article'
filtering = { "title" : ALL }
三.修改article/urls.py的内容如下
from django.conf.urls import patterns, include, url
from api import ArticleResource
article_resource = ArticleResource()
urlpatterns = patterns('',
url(r'^all/$', 'article.views.articles'),
url(r'^get/(?P<article_id>\d+)/$', 'article.views.article'),
url(r'^language/(?P<language>[a-z\-]+)/$', 'article.views.language'),
url(r'^create/$', 'article.views.create'),
url(r'^like/(?P<article_id>\d+)/$', 'article.views.like_article'),
url(r'^add_comment/(?P<article_id>\d+)/$', 'article.views.add_comment'),
url(r'^delete_comment/(?P<comment_id>\d+)/$', 'article.views.delete_comment'),
url(r'^search/$', 'article.views.search_titles'),
url(r'^api/', include(article_resource.urls)),
)
四.更新数据库
python manage.py syncdb
五.最终效果如下