質問

The django rest framework release notes claim that JSONP support was added back in 0.3.0. I also found a JSONPRenderer class in the framework. However, I can't seem to find any documentation on how to use the thing... I'm fairly new to jsonp.

Has anyone ever successfully used jsonp with the django rest framework?

役に立ちましたか?

解決

While posting this, I found the answer (or at least AN answer). It appears that the jsonp renderer is available by default on a ModelResource, so all you need to do is add "?format=json-p" to the requesting url.

他のヒント

Just in case someone is looking for jsonp.

First (docs):

pip install djangorestframework-jsonp

And then modify your REST framework settings.

REST_FRAMEWORK = {
  'DEFAULT_RENDERER_CLASSES': (
    'rest_framework_jsonp.renderers.JSONPRenderer',
  ),
}

Finally make sure that your url contains ?format=jsonp and not ?format=json-p.

from rest_framework.views import APIView
from rest_framework_jsonp.renderers import JSONPRenderer, JSONRenderer
from rest_framework.response import Response

class YourClass(APIView):
    renderer_classes = (JSONPRenderer, JSONRenderer)

    def get(self, request, *args, **kwargs):
        your_result = {{ your serialized result }}
        return Response({'status': 'success', 'result': your_result})
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top