tastypie is a good thing.

Haven't test it thoroughly. Gonna need some provement.

Now I will introduct how to use tastepie for newbies.

Let me introduce all the equipments I have to deploy tastypie.

1) linuxmint 13

2) virtualenv ( sudo apt-get install python-virtualenv )

3) install django==1.5 in the virtualenv folder ( ./bin/pip install django==1.5 )

4) install django-tastypie (./bin/pip install django-tastypie )

5) create new project ( ./bin/djangoadmin.py startproject ts2 )

6) get into the 'ts2' folder ( cd ts2 )

7) create new app (../bin/djangoadmin.py startapp myapp    NOTICE: folder myapp is at the same folder level as manage.py )

8) edit file    ts2/myapp/models.py as following,

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)

9) create ts2/myapp/api.py like this,

# myapp/api.py
from tastypie.resources import ModelResource
from models import Entry # Here i corrected it. the docs from tastypieapi.org is
                     #\ wrong that from myapp.models import Entry class EntryResource(ModelResource):
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'

10) add 2 rows in the ts2/ts2/settings.py as this,

add 'tastypie' and 'myapp' in the INSTALLED_APPS ,

11) edit ts2/ts2/urls.py ,

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover
from myapp.api import EntryResource
entry_resource = EntryResource() urlpatterns = patterns('',
# Examples:
# url(r'^$', 'ts2.views.home', name='home'),
# url(r'^ts2/', include('ts2.foo.urls')), # 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)),
# The normal jazz here...
#(r'^blog/', include('myapp.urls')),
(r'^api/', include(entry_resource.urls)),
)

12) initialize database , forms, and data ( ../bin/python manage.py syncdb )

File struture of this project:

ts2/
|-- db.sqlite3
|-- manage.py
|-- myapp
| |-- api.py
| |-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
`-- ts2
|-- __init__.py
|-- settings.py
|-- urls.py
|-- wsgi.py

13) run server ( ../bin/python manage runserver )

go to http://localhost:8000/api/entry/?format=json

Now you will see this:

This help you to read JSON ! :)

http://json.parser.online.fr/

Isn't good? :P

Before awaring of tastypie, I used orignal JSON parser and 3rd party ORM to do things like REST application works.

I have also heard of django-gap which is more lighter than tastypie.

Gonna have a comparison if possible. :)

Happy Coding!

tastypie Django REST framework API [Hello JSON]的更多相关文章

  1. Django Rest Framework API指南

    Django Rest Framework API指南 Django Rest Framework 所有API如下: Request 请求 Response 响应 View 视图 Generic vi ...

  2. Django REST Framework API Guide 06

    本节大纲 1.Validators 2.Authentication Validators 在REST框架中处理验证的大多数时间,您将仅仅依赖于缺省字段验证,或在序列化器或字段类上编写显式验证方法.但 ...

  3. Django REST Framework API Guide 01

    之前按照REST Framework官方文档提供的简介写了一系列的简单的介绍博客,说白了就是翻译了一下简介,而且翻译的很烂.到真正的生产时,就会发现很鸡肋,连熟悉大概知道rest framework都 ...

  4. Django REST Framework API Guide 04

    本节大纲 1.serializers 1.Serializers Serializers允许复杂的数据,像queryset和模型实例转换成源生的Python数据类型.从而可以更简单的被渲染成JSON, ...

  5. Django REST Framework API Guide 03

    本节大纲 1.Routers 2.Parsers 3.Renderers Routers Usage from rest_framework import routers router = route ...

  6. tastypie Django REST framework

    Its one of the primary authors' lecture on pyCon: http://www.youtube.com/watch?v=Zv26xHYlc8s&nor ...

  7. Django REST Framework API Guide 02

    本节大纲 1.Generic Views 2.ViewSets  1.Generic Views CBV的主要的一个优点就是极大的允许了对于代码的从用.自然,rest framework取其优势,提供 ...

  8. Django REST framework API开发

    RESTful设计方法 1. 域名 应该尽量将API部署在专用域名之下. https://api.example.com 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https:/ ...

  9. Django REST Framework API Guide 08

    1.Filtering 2.Pagination FIltering GenericAPIView的子类筛选queryset的简单方法是重写.get_quueryset()方法. 1.根据当前用户进行 ...

随机推荐

  1. 直读Innodb datafile

    这两天有空翻了翻大神写的<innodb存储引擎>,手痒亲身实践.由于此书出版了有段时日,没有用其推荐的python工具,通过点滴推敲,略微发现其中冰山一角的奥秘.对于今后对于一些问题查证或 ...

  2. C# 窗口间传递数据

    C#两个窗口之间传递数据 1 公用变量值传递 public partial class Form1 : Form //parent form { public string name="&q ...

  3. SQL 无限级分类语句

    原文:SQL 无限级分类语句 原表数据为: 此处用到了with关键字,在程序中也可以用递归实现,但觉得还是没有一条sql方便 with tb (ID,Name,ParentID,Sort) as( s ...

  4. 用javascript把扑克牌理理顺!

    打扑克的人都知道,比如斗地主! 我们一般都会按照顺序把随机摸过来的牌从小到大的顺序在手上理整齐(记得小时候打牌两副牌手都抓不过来),这篇随笔就是想通过实现这个功能来熟悉下js中排序数组等相关知识. 用 ...

  5. 萧墙HTML5手机发展之路(53)——jQueryMobile页面之间的参数传递

    基于单个页面模板HTTP通过路POST和GET请求传递参数.在多页模板,并且不需要server沟通,通常有三种方式在多页模板来实现页面之间的参数传递. 1.GET道路:上一页页生成参数并传递到下一个页 ...

  6. openwrt驱动与应用程序的联系

    应用程序与驱动之间需要进行命令的传递,因而它们之间需要共同定义一套双方都可以识别的数据结构,实际使用时它们include的是名字和内容相同但位置不同的头文件. 比如spi_gpio_ad7193.h这 ...

  7. 读书笔记—CLR via C#字符串及文本

    前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...

  8. Function.prototype.toString

    语法:fn.toString(indentation) 改方法返回当前函数源代码的字符串,而且还可对此字符串进行操作,比如: function num(){ }; var str = num.toSt ...

  9. ASP.NET MVC Model绑定

    ASP.NET MVC Model绑定(一) 前言 ModelMetadata系列的结束了,从本篇开始就进入Model绑定部分了,这个系列阅读过后你会对Model绑定有个比较清楚的了解, 本篇对于Mo ...

  10. Zabbix监控系统功能及基本使用

    一.Zabbix基本介绍:    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.它能监视各种网络参数,保证服务器系统的安全运营:并提供柔软的通知机制以让系 ...