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. php设计模式(二):结构模式

    上一篇我们介绍了设计模式的特性并且详细讲解了4种创建型模式,创建型模式是负责如何产生对象实例的,现在我们继续来给大家介绍结构型模式. 一.什么是结构型模式? 结构型模式是解析类和对象的内部结构和外部组 ...

  2. android KK版本号,如何更改蓝牙设备类型

    mediatek/external/bluetooth/bt_cust/bt_cust_table.h   {         .name = "ClassOfDevice",   ...

  3. 新秀nginx源代码分析数据结构篇(两) 双链表ngx_queue_t

    nginx源代码分析数据结构篇(两) 双链表ngx_queue_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...

  4. Linux server关闭自己主动

    公司linux server发生错误.mysql server没有理由关闭,我找不到理由.Version: '5.6.13-enterprise-commercial-advanced' socket ...

  5. java 中关于json的使用方法

    json在数据传输起了很大的作用,下面说说java中json的使用方法. 文章参考:http://www.codes51.com/article/detail_99574.html json串示例 [ ...

  6. 接口自动化测试:参数化封装(excel文件读取)

    log4j.properties文件配置 log4j.rootLogger = DEBUG,stdout,F log4j.appender.stdout = org.apache.log4j.Cons ...

  7. 原生js写的一个弧形菜单插件

    弧形菜单是一种半弧式或者全弧形菜单,是一种不同于传统横向或者竖向菜单形式的菜单.最近在网上看到好多人写出了这种效果,于是也尝试自己写了一个. 实现方式:原生态js 主要结构: 1.参数合并 var d ...

  8. 3. SQL Server数据库状态监控 - 可用空间

    原文:3. SQL Server数据库状态监控 - 可用空间 数据库用来存放数据,那么肯定需要存储空间,所以对磁盘空间的监视自然就很有必要了. 一. 磁盘可用空间 1. 操作系统命令或脚本.接口或工具 ...

  9. MVC+Bootstrap设计

    MVC+Bootstrap) 二 框架设计 文章目录: 一.前言 二.结构图 三.项目搭建 四.代码生成 五.实现接口 六.依赖倒置 七.登录实现 八.最后 一.前言 这个框架是从最近几年做过的项目中 ...

  10. table居中方法之一:设置width,然后为style设置margin:auto

    比如: <table width="800px" style="margin:auto;">