1. Josh Ourisman
  2. Django-Piston Presentation

Wiki

Clone wiki

Django-Piston Presentation / Home

                             piston-demo
                             ===========

Author: Joshua Ourisman <jourisma@whql80939m.local>
Date: 2009-09-24 13:52:52 EDT


Table of Contents
=================
1 django-piston presentation for django-district meeting 
    1.1 Create Django Project 
    1.2 Set up demoapp 
    1.3 Set up piston 
    1.4 Test 
    1.5 Emitters 


1 [django-piston] presentation for django-district meeting 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1.1 Create Django Project 
==========================
1. mkvirtualenv piston-demo
2. cdvirtualenv
3. pip install django django-piston
4. django-admin.py startproject piston_demo
5. cd piston_demo
6. chmod 774 manage.py
7. ./manage.py startapp demoapp
8. mate .
9. edit settings.py
   1. DATABASE_ENGINE = 'sqlite3'
   2. DATABASE_NAME = 'piston-demo.db'
   3. INSTALLED_APPS += ('django.contrib.admin', 'piston', 'demoapp',)
10. edit urls.py
     1. uncomment lines 4, 5, & 16 to enable admin

1.2 Set up demoapp 
===================
1. create model DemoModel
2. add some fields
   1. name = models.CharField(blank=True, max_length=80)
   2. email = models.EmailField(blank=True)
   3. birthday = models.DateField(blank=True, null=True)
3. set up admin.py
   1. from django.contrib import admin
   2. from models import DemoModel
   3. admin.site.register(DemoModel)
4. set up modelform
   1. from django import forms
   2. from models import DemoModel
   3. class DemoModelForm(forms.ModelForm)
5. ./manage.py syncdb
   1. create admin user

1.3 Set up piston 
==================
1. create handlers.py
   1. from piston.handler import BaseHandler
   2. from piston.utils import rc
   3. from models import *
2. create DemoHandler class
   1. allowed_methods = ('POST', 'GET', 'DELETE',)
   2. create relevant handler methods
      1. def create(self, request)
      2. def read(self, request, object_id)
      3. def delete(self, request, object_id)
3. link piston handlers to urls
   1. from piston.resource import Resource
   2. from demoapp.handlers import DemoHandler
   3. demo_handler = Resource(DemoHandler)
   4. (r'^api/$', demo_handler)
   5. (r'^api/(?P<object_id>\d+)/$', demo_handler)

1.4 Test 
=========
1. ./manage.py runserver
2. curl localhost:8000/api/ -F "name=Josh" -F "email=josh@joshourisman.com" -F "birthday=1982-10-07"
3. curl localhost:8000/api/<object_id>/
4. curl -X DELETE localhost:8000/api/<object_id>/

1.5 Emitters 
=============
1. curl localhost:8000/api/2/?format=xml
2. (r'^api/(?P<object_id>\d+)/$', demo_handler, { 'emitter_format': 'xml' }),

Updated