zoukankan      html  css  js  c++  java
  • 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!

  • 相关阅读:
    阿里云服务器完全卸载监控教程
    培养孩子专注力的10种方法
    多头数据分析
    腾讯分数分析报告-医美
    Omnibus test
    个股与指数的回归分析(自带python ols 参数解读)
    excel多元回归-系数参数解读
    比萨斜塔——统计显著性检验
    how to calculate the best fit to a plane in 3D, and how to find the corresponding statistical parameters
    sns.pairplot
  • 原文地址:https://www.cnblogs.com/spaceship9/p/3474364.html
Copyright © 2011-2022 走看看