gpt4 book ai didi

python - django-piston 中的处理程序错误

转载 作者:行者123 更新时间:2023-12-01 05:59:58 25 4
gpt4 key购买 nike

当我为 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),它就可以工作,但我真的需要它......

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

提前致谢

最佳答案

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

def DevDataHandler(BaseHandler):

至:

class DevDataHandler(BaseHandler):

关于python - django-piston 中的处理程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10957743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com