2

Actually i return json from some media files depends on url <str:scene> varialbe is set. I just want to use rest framework interface and auth token so i think that could be easier way to present my data. Here what i do:

views.py

from rest_framework import views
from django.http import JsonResponse

class Manager(views.APIView):
    def get(self, request, scene):
        return JsonResponse({'get': scene})

    def post(self, request, scene):
        return JsonResponse({'post': scene})

urls.py

router = routers.DefaultRouter()
router.register(r'', views.Manager)

urlpatterns = [
    path('<str:scene>', include(router.urls)),
]

This code requires me to use basename argument in path but since i don't use models i haven't anything to pass in it.

So what is the way to return my custom json data using rest framework interface?

1
  • This post has 500+ views 3 answers with one accepted and 0 votes. Everything is clean in it it's not a duplicate it implements the official recommendations and I believe it helped at least to 10% of it's viewers and it has 0 votes.. and now I have the posting ban because "asking questions is privilege not a right". But I always have like -2 -3 votes with 20-30 viewers if I did some mistake or I was not 100% origin. Thanks you so much. Commented May 29, 2021 at 19:28

3 Answers 3

2

You can't add the APIView to the routers as the routers are meant for ViewSet classes. Also, in DRF, it is good to use the Response class instead of JsonResponse

from rest_framework import views
from rest_framework.response import Response


class Manager(views.APIView):
    def get(self, request, scene):
        return Response({'get': scene})

    def post(self, request, scene):
        return Response({'post': scene})

Now, change the URL patterns as,

urlpatterns = [
    path('<str:scene>', Manager.as_view(), name='manager-view'),
]
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is already giving out json data

2 Comments

No, actually it gives me AssertionError cause i don't specify basename in path
Your base url should be something like this from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path("api/", include("your_app_name.urls")) ] In your app url do this.. app_name = "your app name" urlpatterns = [ # path("pathname/<str:scene>", view_class_name.as_viee, name="view-name") ]
0

I don't know where this JsonResponse comes from (if it's a dependency) but the DRF has the Response that will return the json it needs.

from rest_framework.response import Response

class Manager(views.APIView):

    def get(self, request, scene):
        return Response({'get': scene})

    def post(self, request, scene):
        return Response({'post': scene})

1 Comment

Thanks, but it still doesn't work since i don't specify basename in path

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.