2011-02-20 15 views

回答

4

Django-piston尊重HTTP狀態代碼並默認處理常見錯誤(auth等),但您可以還會使用piston.utils中的rc引發新的異常或狀態。

例如:

from django.contrib.auth.models import User 
from piston.handler import AnonymousBaseHandler 
from piston.utils import rc 

class AnonymousUserHandler(AnonymousBaseHandler): 
    allowed_methods = ('GET',) 
    fields = ('id', 'username',) 

    def read(self, request): 
     try: 
      user = User.objects.get(username=request.GET.get('username', None)) 
      return user 
     except Exception: 
      resp = rc.NOT_FOUND 
      resp.write(' User not found') 
      return resp 

檢查了所有公用事業單位以https://bitbucket.org/jespern/django-piston/wiki/Documentation#!helpers-utils-decorators