tastypie Django REST framework API [Hello JSON]
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]的更多相关文章
- Django Rest Framework API指南
Django Rest Framework API指南 Django Rest Framework 所有API如下: Request 请求 Response 响应 View 视图 Generic vi ...
- Django REST Framework API Guide 06
本节大纲 1.Validators 2.Authentication Validators 在REST框架中处理验证的大多数时间,您将仅仅依赖于缺省字段验证,或在序列化器或字段类上编写显式验证方法.但 ...
- Django REST Framework API Guide 01
之前按照REST Framework官方文档提供的简介写了一系列的简单的介绍博客,说白了就是翻译了一下简介,而且翻译的很烂.到真正的生产时,就会发现很鸡肋,连熟悉大概知道rest framework都 ...
- Django REST Framework API Guide 04
本节大纲 1.serializers 1.Serializers Serializers允许复杂的数据,像queryset和模型实例转换成源生的Python数据类型.从而可以更简单的被渲染成JSON, ...
- Django REST Framework API Guide 03
本节大纲 1.Routers 2.Parsers 3.Renderers Routers Usage from rest_framework import routers router = route ...
- tastypie Django REST framework
Its one of the primary authors' lecture on pyCon: http://www.youtube.com/watch?v=Zv26xHYlc8s&nor ...
- Django REST Framework API Guide 02
本节大纲 1.Generic Views 2.ViewSets 1.Generic Views CBV的主要的一个优点就是极大的允许了对于代码的从用.自然,rest framework取其优势,提供 ...
- Django REST framework API开发
RESTful设计方法 1. 域名 应该尽量将API部署在专用域名之下. https://api.example.com 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https:/ ...
- Django REST Framework API Guide 08
1.Filtering 2.Pagination FIltering GenericAPIView的子类筛选queryset的简单方法是重写.get_quueryset()方法. 1.根据当前用户进行 ...
随机推荐
- 程序员面试必备经典CTCI,谷歌面试官经典作品!
1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除字符串中重复字符 1.8 利用已知函数判断字符串是否为另一字符串的子串 2.1 从链表中移除重复结点 2.2 实现一个算法从一个 ...
- userAgent,JS这么屌的用户代理,你造吗?——判断浏览器内核、浏览器、浏览器平台、windows操作系统版本、移动设备、游戏系统
1.识别浏览器呈现引擎 为了不在全局作用域中添加多余变量,这里使用单例模式(什么是单例模式?)来封装检测脚本.检测脚本的基本代码如下所示: var client = function() { var ...
- NYOJ 14 场地安排(它可以被视为一个经典问题)
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 学校的小礼堂每天都会有很多活动.有时间这些活动的计划时间会发生冲突,须要选择出一些活动进行举办.小刘的工 ...
- oracle存储过程+游标处理select数据
create or replace PROCEDURE UPDATE_RECORDCODE is cursor location_data is select * from location wher ...
- Hbuilder常用快捷键功能.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- windows 7 telnet 开启关闭
win7运行telnet提示:'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件 原因:win7默认没有打开此功能 解决方案:控制面板->程序和功能->打开或关闭wind ...
- leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
- 【转】Android的Merge讲解与实例
原文:http://blog.sina.com.cn/s/blog_62f987620100sf13.html 单独将<merge />标签做个介绍,是因为它在优化UI结构时起到很重要的作 ...
- 站点维护使用app_offline.htm页面提供友好的更新提示
进行站点维护时为了以一个友好的方式提示给用户,比如什么“本网站正在更新”等等的信息可以建立一个叫app_offline.htm 的静态HTM页面文件,其中修改成你要临时显示的内容,将其放在你的应用的根 ...
- Ibatis.Net执行Sql超时commandTimeout的一个坑
项目中使用了Ibatis.Net,数据库是Mysql,在做一个批量Update的操作时,需要执行40几秒,在执行到30秒的时候,会抛出异常:Timeout expired , The timeout ...