0

ORM のモデルに関連付けられていないカスタム ハンドラー ( CustomHandler) を作成しました。これは正しく設定されていると思いますが、ImportError: cannot import CustomHandlerそれを自分のresources.py. これが私のセットアップです:

custom_handlers.py :

from piston.handler import BaseHandler

class CustomHandler(BaseHandler):
    allowed_methods = ('GET',)

    def read(self, request):
        return 'test'

resources.py :

from piston.resource import Resource
from piston.utils import rc
import simplejson as json
from api.authentication import DjangoAuthentication
from api.handlers import CustomHandler # ERROR THROWN HERE

auth = DjangoAuthentication(realm='...')

class JSONResource(Resource):
    def determine_emitter(self, request, *args, **kwargs):
        """
        Default to the json emitter.
        """
        try:
            return kwargs['emitter_format']
        except KeyError:
            pass
        if 'format' in request.GET:
            return request.GET.get('format')
        return 'json'

    def form_validation_response(self, e):
        """
        Turns the error object into a serializable construct.
        """
        resp = rc.BAD_REQUEST
        json_errors = json.dumps(
            dict(
                (k, map(unicode, v))
                for (k, v) in e.form.errors.iteritems()
            )
        )
        resp.write(json_errors)
        return resp


custom_handler = JSONResource(CustomHandler, authentication=auth)

urls.py :

from django.conf.urls.defaults import patterns, url

from api.resources import custom_handler

urlpatterns = patterns('',
    url(r'^things/$', custom_handler),
)

更新:py手動でs をs にコンパイルしようとしましたがpyc、うまくいきませんでした。ピストンのドキュメントでもこれを読みました:

モデルに関連付けられたハンドラーを作成すると、Piston はそれを (メタクラスを介して) 自動的に登録します。

しかし、モデルに関連付けられていないハンドラーの作成、特に登録方法に関するドキュメントには何も見つかりません。

4

1 に答える 1

0

に追加するfrom api.handlers.custom_handlers import CustomHandler必要がありましたapi/handlers/__init__.py

于 2013-02-05T19:25:33.783 に答える