Django 1.5.4 专题13 Web Service with tastypie

一.首先安装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

五.最终效果如下







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值