Using jsonp with django-rest-framework

The django rest database release notes state that JSONP support was added back to 0.3.0. I also found the JSONPRenderer class within the framework. However, I cannot find any documentation on how to use this thing ... I am new to jsonp.

Has anyone ever successfully used jsonp with a django leisure map?

+6
source share
3 answers

While posting this question, I found an answer (or at least AN answer). It seems that jsonp rendering is available by default on ModelResource, so all you have to do is add "? Format = json-p" to the requesting url.

+5
source

Just in case, someone is looking for jsonp .

First ( docs ):

pip install djangorestframework-jsonp

And then change the settings of the REST environment.

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

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

+2
source
 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}) 
0
source

Source: https://habr.com/ru/post/922134/


All Articles