1

当我为我的 django-piston 实现另一个处理程序时,我在我的 django 项目中遇到了这个错误。

Request Method: GET
Request URL: http://127.0.0.1:8000/api/getdata/

Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['admin_tools.theming',
 'admin_tools.menu',
 'admin_tools.dashboard',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'app',
 'api',
 'south']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.csrf.CsrfResponseMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  252.                     sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  250.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_url_patterns
  279.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_urlconf_module
  274.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/agileone/workspace/proj/api/urls.py" in <module>
  15. devdata_handler = Resource(DevDataHandler)
File "/usr/local/lib/python2.7/dist-packages/django_piston-0.3dev-py2.7.egg/piston/resource.py" in __init__
  36.         self.handler = handler()

Exception Type: TypeError at /api/getdata/
Exception Value: DevDataHandler() takes exactly 1 argument (0 given)

我不知道我得到这些的原因是什么。

这是我的api/urls.py

from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import *

from django.http import HttpResponse
from piston.handler import BaseHandler, AnonymousBaseHandler


class CsrfExemptResource(Resource):
    def __init__(self, handler, authentication=None):
        super(CsrfExemptResource,self).__init__(handler,authentication)
        self.csrf_exempt=getattr(self.handler,'csrf_exempt',True)

data_handler = CsrfExemptResource(DataHandler)
devdata_handler = Resource(DevDataHandler)
urlpatterns=patterns('',
    url(r'^getdata/$', data_handler,  {'emitter_format': 'ext-json'}),
    url(r'^getdevdata/$', devdata_handler,  {'emitter_format': 'ext-json'})
)

这是我的api/handlers.py

from django.utils import simplejson
from piston.handler import BaseHandler, AnonymousBaseHandler
from app.models import *
from django.db.models import *
from piston.utils import rc, require_mime, require_extended, validate
import datetime

class DataHandler(BaseHandler):
    allowed_method = ('GET', 'POST', 'PUT', 'DELETE')
    fields = ('title')
    model = Data

    def read(self, request): 
        data = {"msg":"Hello world"}
        return data        

def DevDataHandler(BaseHandler):
    allowed_method = ('GET', 'POST', 'PUT', 'DELETE')
    fields = ('title')
    model = Data

    def read(self, request): 
        data = {"msg":"Hi world"}
        return data

如果我不包括它,它会起作用,devdata_handler = Resource(DevDataHandler)但我真的需要它......

有人可以帮我解决我的问题吗?

提前致谢

4

1 回答 1

2

处理程序是类,而不是函数。改变:

def DevDataHandler(BaseHandler):

至:

class DevDataHandler(BaseHandler):
于 2012-07-30T22:55:54.310 回答