Reputation: 8065
I am fairly new to Django and am trying to implement a basic REST API in Django. I am having a news list in MySQL Database, and following various tutorials, successfully managed to implement a webservice that responds the news list.
http://127.0.0.1:8000/rest/news_infos/
The above URL(local) produces the following page:
As you can see, I am getting a webpage here. But what I actually needed is that the above API to return just the json. I don't want to have this kind of pages available in my webservice, just the JSON response. Of course I can get it json only by appending format=json
to the request. But that is not what I require. I want the web page to be gone and calling http://127.0.0.1:8000/rest/news_infos/
to return the json instead.
Following is my views.py code:
from rest_framework import viewsets
from .models import NewsContent, NewsInfo
from .serializers import NewsContentSerializer, NewsInfoSerializer
class NewsContentViewSet(viewsets.ViewSet):
queryset = NewsContent.objects.all()[:10]
serializer_class = NewsContentSerializer
class NewsInfoViewSet(viewsets.ModelViewSet):
queryset = NewsInfo.objects.all()[:10]
serializer_class = NewsInfoSerializer
Please let me know if any other code/info is required. Couldn't find any proper solution online.
Upvotes: 6
Views: 2545
Reputation: 88519
Change DEFAULT_RENDERER_CLASSES
in settings.py
as
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
)
}
It could be done view level by providing renderer_classes
as,
from rest_framework import renderers
class MyView(...):
renderer_classes = [renderers.JSONRenderer]
Upvotes: 6